From 4b702b6ea0e08a1dd534f68313c9ca2e5ef0c735 Mon Sep 17 00:00:00 2001 From: Chumva Date: Mon, 2 Dec 2019 12:22:53 +0200 Subject: [PATCH 1/6] Make plugins and connected apps global again --- OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 32 ++++--------------- OsmAnd/src/net/osmand/plus/OsmandPlugin.java | 17 ---------- .../src/net/osmand/plus/OsmandSettings.java | 30 +++++------------ .../settings/ConfigureProfileFragment.java | 20 ++++-------- 4 files changed, 20 insertions(+), 79 deletions(-) diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index a2a12b8ff5..1ffff0bccc 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -96,7 +96,6 @@ 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; @@ -1835,8 +1834,7 @@ public class OsmandAidlApi { public boolean switchEnabled(@NonNull ConnectedApp connectedApp) { connectedApp.enabled = !connectedApp.enabled; - ApplicationMode selectedAppMode = app.getSettings().APPLICATION_MODE.get(); - return saveConnectedApps(selectedAppMode, connectedApps); + return saveConnectedApps(); } public boolean isAppEnabled(@NonNull String pack) { @@ -1844,22 +1842,12 @@ public class OsmandAidlApi { if (app == null) { app = new ConnectedApp(pack, true); connectedApps.put(pack, app); - saveNewConnectedApp(app); + saveConnectedApps(); } return app.enabled; } - 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) { + private boolean saveConnectedApps() { try { JSONArray array = new JSONArray(); for (ConnectedApp app : connectedApps.values()) { @@ -1868,7 +1856,7 @@ public class OsmandAidlApi { obj.put(ConnectedApp.PACK_KEY, app.pack); array.put(obj); } - return app.getSettings().API_CONNECTED_APPS_JSON.setModeValue(mode, array.toString()); + return app.getSettings().API_CONNECTED_APPS_JSON.set(array.toString()); } catch (JSONException e) { e.printStackTrace(); } @@ -1876,16 +1864,9 @@ 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 { - JSONArray array = new JSONArray(app.getSettings().API_CONNECTED_APPS_JSON.getModeValue(mode)); + connectedApps.clear(); + JSONArray array = new JSONArray(app.getSettings().API_CONNECTED_APPS_JSON.get()); for (int i = 0; i < array.length(); i++) { JSONObject obj = array.getJSONObject(i); String pack = obj.optString(ConnectedApp.PACK_KEY, ""); @@ -1895,7 +1876,6 @@ public class OsmandAidlApi { } catch (JSONException e) { e.printStackTrace(); } - return connectedApps; } boolean setNavDrawerLogo(@Nullable String uri) { diff --git a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java index 990c39b0bf..2de54cf09d 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java @@ -267,23 +267,6 @@ public abstract class OsmandPlugin { return false; } - public static boolean isPluginEnabledForMode(@NonNull OsmandApplication app, @NonNull OsmandPlugin plugin, @NonNull ApplicationMode mode) { - if (plugin.isMarketPlugin()) { - boolean marketEnabled = Version.isMarketEnabled(app); - boolean pckg = checkPluginPackage(app, plugin); - if ((Version.isDeveloperVersion(app) || !Version.isProductionVersion(app)) && !plugin.isPaid()) { - // for test reasons - marketEnabled = false; - } - if (pckg || (!marketEnabled && !plugin.isPaid())) { - return pckg && !app.getSettings().getPluginsForMode(mode).contains("-" + plugin.getId()); - } - return false; - } else { - return app.getSettings().getEnabledPluginsForMode(mode).contains(plugin.getId()); - } - } - public static void checkInstalledMarketPlugins(@NonNull OsmandApplication app, @Nullable Activity activity) { Set enabledPlugins = app.getSettings().getEnabledPlugins(); for (OsmandPlugin plugin : OsmandPlugin.getMarketPlugins()) { diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 25532f7e41..cdc271044e 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -437,8 +437,6 @@ public class OsmandSettings { OsmandAidlApi aidlApi = ctx.getAidlApi(); if (aidlApi != null) { - aidlApi.loadConnectedApps(); - OsmandPlugin.updateActivatedPlugins(ctx); ctx.poiFilters.loadSelectedPoiFilters(); } @@ -1017,14 +1015,10 @@ public class OsmandSettings { return super.getProfileDefaultValue(mode); } } - }.makeProfile(); + }.makeGlobal(); public Set getEnabledPlugins() { - return getEnabledPluginsForMode(APPLICATION_MODE.get()); - } - - public Set getEnabledPluginsForMode(ApplicationMode mode) { - String plugs = PLUGINS.getModeValue(mode); + String plugs = PLUGINS.get(); StringTokenizer toks = new StringTokenizer(plugs, ","); Set res = new LinkedHashSet(); while (toks.hasMoreTokens()) { @@ -1037,11 +1031,7 @@ public class OsmandSettings { } public Set getPlugins() { - return getPluginsForMode(APPLICATION_MODE.get()); - } - - public Set getPluginsForMode(ApplicationMode mode) { - String plugs = PLUGINS.getModeValue(mode); + String plugs = PLUGINS.get(); StringTokenizer toks = new StringTokenizer(plugs, ","); Set res = new LinkedHashSet(); while (toks.hasMoreTokens()) { @@ -1051,11 +1041,7 @@ public class OsmandSettings { } public boolean enablePlugin(String pluginId, boolean enable) { - return enablePluginForMode(pluginId, enable, APPLICATION_MODE.get()); - } - - public boolean enablePluginForMode(String pluginId, boolean enable, ApplicationMode mode) { - Set set = getPluginsForMode(mode); + Set set = getPlugins(); if (enable) { set.remove("-" + pluginId); set.add(pluginId); @@ -1071,8 +1057,8 @@ public class OsmandSettings { serialization.append(","); } } - if (!serialization.toString().equals(PLUGINS.getModeValue(mode))) { - return PLUGINS.setModeValue(mode, serialization.toString()); + if (!serialization.toString().equals(PLUGINS.get())) { + return PLUGINS.set(serialization.toString()); } return false; } @@ -1172,7 +1158,7 @@ public class OsmandSettings { return super.getProfileDefaultValue(mode); } } - }.makeProfile(); + }.makeGlobal(); public final CommonPreference NUMBER_OF_STARTS_FIRST_XMAS_SHOWN = new IntPreference("number_of_starts_first_xmas_shown", 0).makeGlobal(); @@ -1607,7 +1593,7 @@ public class OsmandSettings { public static final Integer DAILY_DIRECTORY = 2; public final CommonPreference DISABLE_RECORDING_ONCE_APP_KILLED = new BooleanPreference("disable_recording_once_app_killed", false).makeProfile().makeGeneral(); - + public final CommonPreference SAVE_HEADING_TO_GPX = new BooleanPreference("save_heading_to_gpx", false).makeProfile().makeGeneral(); public final CommonPreference TRACK_STORAGE_DIRECTORY = new IntPreference("track_storage_directory", 0).makeProfile().makeGeneral(); diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java index 89abd68987..6000ea9021 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java @@ -262,13 +262,12 @@ public class ConfigureProfileFragment extends BaseSettingsFragment { if (plugin instanceof SkiMapsPlugin || plugin instanceof NauticalMapsPlugin) { continue; } - boolean pluginEnabled = OsmandPlugin.isPluginEnabledForMode(app, plugin, getSelectedAppMode()); SwitchPreferenceEx preference = new SwitchPreferenceEx(ctx); preference.setPersistent(false); preference.setKey(plugin.getId()); preference.setTitle(plugin.getName()); - preference.setChecked(pluginEnabled); - preference.setIcon(getPluginIcon(plugin, pluginEnabled)); + preference.setChecked(plugin.isActive()); + preference.setIcon(getPluginIcon(plugin)); preference.setIntent(getPluginIntent(plugin)); preference.setLayoutResource(R.layout.preference_dialog_and_switch); @@ -276,9 +275,9 @@ public class ConfigureProfileFragment extends BaseSettingsFragment { } } - private Drawable getPluginIcon(OsmandPlugin plugin, boolean pluginEnabled) { + private Drawable getPluginIcon(OsmandPlugin plugin) { int iconResId = plugin.getLogoResourceId(); - return pluginEnabled ? getActiveIcon(iconResId) : getIcon(iconResId, isNightMode() ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light); + return plugin.isActive() ? getActiveIcon(iconResId) : getIcon(iconResId, isNightMode() ? R.color.icon_color_secondary_dark : R.color.icon_color_secondary_light); } private Intent getPluginIntent(OsmandPlugin plugin) { @@ -349,15 +348,8 @@ public class ConfigureProfileFragment extends BaseSettingsFragment { if (plugin != null) { if (newValue instanceof Boolean) { if ((plugin.isActive() || !plugin.needsInstallation())) { - ApplicationMode selectedMode = getSelectedAppMode(); - boolean pluginChanged; - if (selectedMode.equals(settings.APPLICATION_MODE.get())) { - pluginChanged = OsmandPlugin.enablePlugin(getActivity(), app, plugin, (Boolean) newValue); - } else { - pluginChanged = settings.enablePluginForMode(plugin.getId(), (Boolean) newValue, selectedMode); - } - if (pluginChanged) { - preference.setIcon(getPluginIcon(plugin, (Boolean) newValue)); + if (OsmandPlugin.enablePlugin(getActivity(), app, plugin, (Boolean) newValue)) { + preference.setIcon(getPluginIcon(plugin)); return true; } } else if (plugin.needsInstallation() && preference.getIntent() != null) { From c93854eb43461a697f1fb7e1d4f284336594147e Mon Sep 17 00:00:00 2001 From: Chumva Date: Mon, 2 Dec 2019 12:44:23 +0200 Subject: [PATCH 2/6] Revert fix #7789 because plugins became global --- OsmAnd/src/net/osmand/plus/AppInitializer.java | 3 ++- OsmAnd/src/net/osmand/plus/OsmandPlugin.java | 15 --------------- .../net/osmand/plus/activities/MapActivity.java | 1 - .../plus/activities/SavingTrackHelper.java | 16 ++++++++++------ .../plus/monitoring/LiveMonitoringHelper.java | 5 ++++- .../plus/notifications/ErrorNotification.java | 2 +- .../plus/notifications/GpxNotification.java | 8 ++++---- 7 files changed, 21 insertions(+), 29 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index b9e70643ce..da84471693 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -38,6 +38,7 @@ import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.liveupdates.LiveUpdatesHelper; import net.osmand.plus.mapmarkers.MapMarkersDbHelper; import net.osmand.plus.monitoring.LiveMonitoringHelper; +import net.osmand.plus.monitoring.OsmandMonitoringPlugin; import net.osmand.plus.poi.PoiFiltersHelper; import net.osmand.plus.render.MapRenderRepositories; import net.osmand.plus.render.NativeOsmandLibrary; @@ -768,7 +769,7 @@ public class AppInitializer implements IProgress { app.savingTrackHelper.loadGpxFromDatabase(); } } - if (app.savingTrackHelper.getIsRecording()) { + if(app.getSettings().SAVE_GLOBAL_TRACK_TO_GPX.get() && OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null){ int interval = app.getSettings().SAVE_GLOBAL_TRACK_INTERVAL.get(); app.startNavigationService(NavigationService.USED_BY_GPX, app.navigationServiceGpsInterval(interval)); } diff --git a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java index 2de54cf09d..a5c4004aff 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/OsmandPlugin.java @@ -208,21 +208,6 @@ public abstract class OsmandPlugin { } } - public static void updateActivatedPlugins(OsmandApplication app) { - Set enabledPlugins = app.getSettings().getEnabledPlugins(); - for (OsmandPlugin plugin : OsmandPlugin.getMarketPlugins()) { - updateMarketPlugin(app, enabledPlugins, plugin); - } - for (OsmandPlugin plugin : allPlugins) { - if (enabledPlugins.contains(plugin.getId())) { - initPlugin(app, plugin); - } else if (plugin.isActive()) { - plugin.setActive(false); - plugin.disable(app); - } - } - } - private static void initPlugin(OsmandApplication app, OsmandPlugin plugin) { try { if (plugin.init(app, null)) { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 120c8dbc40..41c0d8fae8 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -1516,7 +1516,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven }); getMapView().refreshMap(true); applyScreenOrientation(); - getMyApplication().getNotificationHelper().refreshNotifications(); } public void updateMapSettings() { diff --git a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java index 78ad9d58d9..4e8f2dadc0 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java +++ b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java @@ -5,20 +5,22 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.text.format.DateFormat; +import net.osmand.PlatformUtil; +import net.osmand.data.LatLon; +import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXTrackAnalysis; import net.osmand.GPXUtilities.Track; import net.osmand.GPXUtilities.TrkSegment; import net.osmand.GPXUtilities.WptPt; -import net.osmand.PlatformUtil; -import net.osmand.data.LatLon; -import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OsmAndLocationProvider; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandSettings; import net.osmand.plus.Version; +import net.osmand.plus.monitoring.OsmandMonitoringPlugin; import net.osmand.plus.notifications.OsmandNotification.NotificationType; import net.osmand.util.MapUtils; @@ -426,7 +428,8 @@ public class SavingTrackHelper extends SQLiteOpenHelper { heading = NO_HEADING; } boolean record = false; - if (location != null && OsmAndLocationProvider.isNotSimulatedLocation(location)) { + if (location != null && OsmAndLocationProvider.isNotSimulatedLocation(location) + && OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null) { if (settings.SAVE_TRACK_TO_GPX.get() && locationTime - lastTimeUpdated > settings.SAVE_TRACK_INTERVAL.get() && ctx.getRoutingHelper().isFollowingMode()) { @@ -667,8 +670,9 @@ public class SavingTrackHelper extends SQLiteOpenHelper { } public boolean getIsRecording() { - return ctx.getSettings().SAVE_GLOBAL_TRACK_TO_GPX.get() || - (ctx.getSettings().SAVE_TRACK_TO_GPX.get() && ctx.getRoutingHelper().isFollowingMode()); + return OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null + && ctx.getSettings().SAVE_GLOBAL_TRACK_TO_GPX.get() + || (ctx.getSettings().SAVE_TRACK_TO_GPX.get() && ctx.getRoutingHelper().isFollowingMode()); } public float getDistance() { diff --git a/OsmAnd/src/net/osmand/plus/monitoring/LiveMonitoringHelper.java b/OsmAnd/src/net/osmand/plus/monitoring/LiveMonitoringHelper.java index 4d73b90105..c3f9649a28 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/LiveMonitoringHelper.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/LiveMonitoringHelper.java @@ -7,6 +7,7 @@ import net.osmand.PlatformUtil; import net.osmand.data.LatLon; import net.osmand.plus.OsmAndLocationProvider; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandSettings; import net.osmand.util.MapUtils; @@ -46,7 +47,9 @@ public class LiveMonitoringHelper { public void updateLocation(net.osmand.Location location) { boolean record = false; long locationTime = System.currentTimeMillis(); - if (location != null && isLiveMonitoringEnabled() && OsmAndLocationProvider.isNotSimulatedLocation(location)) { + if (location != null && isLiveMonitoringEnabled() + && OsmAndLocationProvider.isNotSimulatedLocation(location) + && OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null) { if (locationTime - lastTimeUpdated > settings.LIVE_MONITORING_INTERVAL.get()) { record = true; } diff --git a/OsmAnd/src/net/osmand/plus/notifications/ErrorNotification.java b/OsmAnd/src/net/osmand/plus/notifications/ErrorNotification.java index 3ea07aa690..af9a3841c9 100644 --- a/OsmAnd/src/net/osmand/plus/notifications/ErrorNotification.java +++ b/OsmAnd/src/net/osmand/plus/notifications/ErrorNotification.java @@ -62,7 +62,7 @@ public class ErrorNotification extends OsmandNotification { boolean planning = routingHelper.isRoutePlanningMode(); boolean pause = routingHelper.isPauseNavigation(); - boolean gpxEnabled = app.getSavingTrackHelper().getIsRecording() || OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null; + boolean gpxEnabled = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null; String usedBy = service != null ? "" + service.getUsedBy() : "X"; notificationText = "Info: " + (following ? "1" : "") + (planning ? "2" : "") + (pause ? "3" : "") + (gpxEnabled ? "4" : "") + "-" + usedBy + ". " diff --git a/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java b/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java index f0defc9be8..c56b92027b 100644 --- a/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java +++ b/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java @@ -40,7 +40,7 @@ public class GpxNotification extends OsmandNotification { @Override public void onReceive(Context context, Intent intent) { - final OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class); + final OsmandMonitoringPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); if (plugin != null) { plugin.saveCurrentTrack(); if (!app.getSettings().SAVE_GLOBAL_TRACK_TO_GPX.get()) { @@ -54,7 +54,7 @@ public class GpxNotification extends OsmandNotification { @Override public void onReceive(Context context, Intent intent) { - final OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class); + final OsmandMonitoringPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); if (plugin != null) { plugin.startGPXMonitoring(null); plugin.updateControl(); @@ -66,7 +66,7 @@ public class GpxNotification extends OsmandNotification { @Override public void onReceive(Context context, Intent intent) { - final OsmandMonitoringPlugin plugin = OsmandPlugin.getPlugin(OsmandMonitoringPlugin.class); + final OsmandMonitoringPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); if (plugin != null) { plugin.stopRecording(); plugin.updateControl(); @@ -95,7 +95,7 @@ public class GpxNotification extends OsmandNotification { @Override public boolean isEnabled() { - return app.getSavingTrackHelper().getIsRecording() || OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null; + return OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null; } @Override From e95b964f622ab935d78d3601a79191c83ced93c4 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 4 Dec 2019 11:34:03 +0200 Subject: [PATCH 3/6] Aidl layers in configure map initial commit --- .../net/osmand/aidl/OsmandAidlService.java | 35 ++++++++++++------- .../net/osmand/aidl/OsmandAidlServiceV2.java | 35 ++++++++++++------- .../osmand/plus/dialogs/ConfigureMapMenu.java | 1 + .../net/osmand/plus/views/AidlMapLayer.java | 21 ++++++----- 4 files changed, 58 insertions(+), 34 deletions(-) diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java index 7b4dc31c3a..977f20d96e 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlService.java @@ -122,8 +122,8 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { private OsmandAidlApi getApi(String reason) { LOG.info("Request AIDL API for " + reason); OsmandAidlApi api = getApp().getAidlApi(); - String pack = getCallingAppPackName(); - if (pack != null && !pack.equals(getApp().getPackageName()) && !api.isAppEnabled(pack)) { + String packName = getCallingAppPackName(); + if (packName != null && !packName.equals(getApp().getPackageName()) && !api.isAppEnabled(packName)) { return null; } return api; @@ -378,7 +378,8 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { public boolean addMapWidget(AddMapWidgetParams params) { try { OsmandAidlApi api = getApi("addMapWidget"); - return params != null && api != null && api.addMapWidget(new AidlMapWidgetWrapper(params.getWidget())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.addMapWidget(packName, new AidlMapWidgetWrapper(params.getWidget())); } catch (Exception e) { handleException(e); return false; @@ -389,7 +390,8 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { public boolean removeMapWidget(RemoveMapWidgetParams params) { try { OsmandAidlApi api = getApi("removeMapWidget"); - return params != null && api != null && api.removeMapWidget(params.getId()); + String packName = getCallingAppPackName(); + return params != null && api != null && api.removeMapWidget(packName, params.getId()); } catch (Exception e) { return false; } @@ -399,7 +401,8 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { public boolean updateMapWidget(UpdateMapWidgetParams params) { try { OsmandAidlApi api = getApi("updateMapWidget"); - return params != null && api != null && api.updateMapWidget(new AidlMapWidgetWrapper(params.getWidget())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.updateMapWidget(packName, new AidlMapWidgetWrapper(params.getWidget())); } catch (Exception e) { handleException(e); return false; @@ -410,7 +413,8 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { public boolean showMapPoint(ShowMapPointParams params) { try { OsmandAidlApi api = getApi("showMapPoint"); - return params != null && api != null && api.showMapPoint(params.getLayerId(), new AidlMapPointWrapper(params.getPoint())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.showMapPoint(packName, params.getLayerId(), new AidlMapPointWrapper(params.getPoint())); } catch (Exception e) { handleException(e); return false; @@ -421,7 +425,8 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { public boolean addMapPoint(AddMapPointParams params) { try { OsmandAidlApi api = getApi("addMapPoint"); - return params != null && api != null && api.putMapPoint(params.getLayerId(), new AidlMapPointWrapper(params.getPoint())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.putMapPoint(packName, params.getLayerId(), new AidlMapPointWrapper(params.getPoint())); } catch (Exception e) { handleException(e); return false; @@ -432,7 +437,8 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { public boolean removeMapPoint(RemoveMapPointParams params) { try { OsmandAidlApi api = getApi("removeMapPoint"); - return params != null && api != null && api.removeMapPoint(params.getLayerId(), params.getPointId()); + String packName = getCallingAppPackName(); + return params != null && api != null && api.removeMapPoint(packName, params.getLayerId(), params.getPointId()); } catch (Exception e) { handleException(e); return false; @@ -443,7 +449,8 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { public boolean updateMapPoint(UpdateMapPointParams params) { try { OsmandAidlApi api = getApi("updateMapPoint"); - return params != null && api != null && api.updateMapPoint(params.getLayerId(), new AidlMapPointWrapper(params.getPoint()), params.isUpdateOpenedMenuAndMap()); + String packName = getCallingAppPackName(); + return params != null && api != null && api.updateMapPoint(packName, params.getLayerId(), new AidlMapPointWrapper(params.getPoint()), params.isUpdateOpenedMenuAndMap()); } catch (Exception e) { handleException(e); return false; @@ -454,8 +461,8 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { public boolean addMapLayer(AddMapLayerParams params) { try { OsmandAidlApi api = getApi("addMapLayer"); - String pack = getCallingAppPackName(); - return params != null && api != null && api.addMapLayer(pack, new AidlMapLayerWrapper(params.getLayer())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.addMapLayer(packName, new AidlMapLayerWrapper(params.getLayer())); } catch (Exception e) { handleException(e); return false; @@ -466,7 +473,8 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { public boolean removeMapLayer(RemoveMapLayerParams params) { try { OsmandAidlApi api = getApi("removeMapLayer"); - return params != null && api != null && api.removeMapLayer(params.getId()); + String packName = getCallingAppPackName(); + return params != null && api != null && api.removeMapLayer(packName, params.getId()); } catch (Exception e) { handleException(e); return false; @@ -477,7 +485,8 @@ public class OsmandAidlService extends Service implements AidlCallbackListener { public boolean updateMapLayer(UpdateMapLayerParams params) { try { OsmandAidlApi api = getApi("updateMapLayer"); - return params != null && api != null && api.updateMapLayer(new AidlMapLayerWrapper(params.getLayer())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.updateMapLayer(packName, new AidlMapLayerWrapper(params.getLayer())); } catch (Exception e) { handleException(e); return false; diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java index bb1c7676f5..37b773891c 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java @@ -123,8 +123,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener private OsmandAidlApi getApi(String reason) { LOG.info("Request AIDL API V2 for " + reason); OsmandAidlApi api = getApp().getAidlApi(); - String pack = getCallingAppPackName(); - if (pack != null && !pack.equals(getApp().getPackageName()) && !api.isAppEnabled(pack)) { + String packName = getCallingAppPackName(); + if (packName != null && !packName.equals(getApp().getPackageName()) && !api.isAppEnabled(packName)) { return null; } return api; @@ -378,7 +378,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener public boolean addMapWidget(AddMapWidgetParams params) { try { OsmandAidlApi api = getApi("addMapWidget"); - return params != null && api != null && api.addMapWidget(new AidlMapWidgetWrapper(params.getWidget())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.addMapWidget(packName, new AidlMapWidgetWrapper(params.getWidget())); } catch (Exception e) { handleException(e); return false; @@ -389,7 +390,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener public boolean removeMapWidget(RemoveMapWidgetParams params) { try { OsmandAidlApi api = getApi("removeMapWidget"); - return params != null && api != null && api.removeMapWidget(params.getId()); + String packName = getCallingAppPackName(); + return params != null && api != null && api.removeMapWidget(packName, params.getId()); } catch (Exception e) { return false; } @@ -399,7 +401,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener public boolean updateMapWidget(UpdateMapWidgetParams params) { try { OsmandAidlApi api = getApi("updateMapWidget"); - return params != null && api != null && api.updateMapWidget(new AidlMapWidgetWrapper(params.getWidget())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.updateMapWidget(packName, new AidlMapWidgetWrapper(params.getWidget())); } catch (Exception e) { handleException(e); return false; @@ -410,7 +413,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener public boolean showMapPoint(ShowMapPointParams params) { try { OsmandAidlApi api = getApi("showMapPoint"); - return params != null && api != null && api.showMapPoint(params.getLayerId(), new AidlMapPointWrapper(params.getPoint())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.showMapPoint(packName, params.getLayerId(), new AidlMapPointWrapper(params.getPoint())); } catch (Exception e) { handleException(e); return false; @@ -421,7 +425,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener public boolean addMapPoint(AddMapPointParams params) { try { OsmandAidlApi api = getApi("addMapPoint"); - return params != null && api != null && api.putMapPoint(params.getLayerId(), new AidlMapPointWrapper(params.getPoint())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.putMapPoint(packName, params.getLayerId(), new AidlMapPointWrapper(params.getPoint())); } catch (Exception e) { handleException(e); return false; @@ -432,7 +437,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener public boolean removeMapPoint(RemoveMapPointParams params) { try { OsmandAidlApi api = getApi("removeMapPoint"); - return params != null && api != null && api.removeMapPoint(params.getLayerId(), params.getPointId()); + String packName = getCallingAppPackName(); + return params != null && api != null && api.removeMapPoint(packName, params.getLayerId(), params.getPointId()); } catch (Exception e) { handleException(e); return false; @@ -443,7 +449,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener public boolean updateMapPoint(UpdateMapPointParams params) { try { OsmandAidlApi api = getApi("updateMapPoint"); - return params != null && api != null && api.updateMapPoint(params.getLayerId(), new AidlMapPointWrapper(params.getPoint()), params.isUpdateOpenedMenuAndMap()); + String packName = getCallingAppPackName(); + return params != null && api != null && api.updateMapPoint(packName, params.getLayerId(), new AidlMapPointWrapper(params.getPoint()), params.isUpdateOpenedMenuAndMap()); } catch (Exception e) { handleException(e); return false; @@ -454,8 +461,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener public boolean addMapLayer(AddMapLayerParams params) { try { OsmandAidlApi api = getApi("addMapLayer"); - String pack = getCallingAppPackName(); - return params != null && api != null && api.addMapLayer(pack, new AidlMapLayerWrapper(params.getLayer())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.addMapLayer(packName, new AidlMapLayerWrapper(params.getLayer())); } catch (Exception e) { handleException(e); return false; @@ -466,7 +473,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener public boolean removeMapLayer(RemoveMapLayerParams params) { try { OsmandAidlApi api = getApi("removeMapLayer"); - return params != null && api != null && api.removeMapLayer(params.getId()); + String packName = getCallingAppPackName(); + return params != null && api != null && api.removeMapLayer(packName, params.getId()); } catch (Exception e) { handleException(e); return false; @@ -477,7 +485,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener public boolean updateMapLayer(UpdateMapLayerParams params) { try { OsmandAidlApi api = getApi("updateMapLayer"); - return params != null && api != null && api.updateMapLayer(new AidlMapLayerWrapper(params.getLayer())); + String packName = getCallingAppPackName(); + return params != null && api != null && api.updateMapLayer(packName, new AidlMapLayerWrapper(params.getLayer())); } catch (Exception e) { handleException(e); return false; diff --git a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java index a9947bf290..a1e062057f 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java @@ -541,6 +541,7 @@ public class ConfigureMapMenu { srtmPlugin.registerLayerContextMenuActions(activity.getMapView(), adapter, activity); } } + app.getAidlApi().registerLayerContextMenu(activity.getMapView(), adapter, activity); } public static void refreshMapComplete(final MapActivity activity) { diff --git a/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java b/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java index 509cbeb98e..51f86b56ec 100644 --- a/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java @@ -22,6 +22,8 @@ import net.osmand.aidlapi.maplayer.point.AMapPoint; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.data.RotatedTileBox; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings.CommonPreference; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MapContextMenu; @@ -54,6 +56,9 @@ public class AidlMapLayer extends OsmandMapLayer implements IContextMenuProvider private String packName; private AidlMapLayerWrapper aidlLayer; + private CommonPreference layerPref; + private CommonPreference appLayersPref; + private Paint pointInnerCircle; private Paint pointOuterCircle; private Paint bitmapPaint; @@ -82,6 +87,10 @@ public class AidlMapLayer extends OsmandMapLayer implements IContextMenuProvider this.map = map; this.aidlLayer = aidlLayer; this.packName = packName; + + OsmandApplication app = map.getMyApplication(); + layerPref = app.getSettings().registerBooleanPreference(packName + "_" + aidlLayer.getId(), true); + appLayersPref = app.getSettings().registerBooleanPreference("layers_" + packName, true); } @Override @@ -139,7 +148,7 @@ public class AidlMapLayer extends OsmandMapLayer implements IContextMenuProvider displayedPoints.clear(); imageRequests.clear(); - if (isAppEnabled()) { + if (isLayerEnabled()) { canvas.rotate(-tileBox.getRotate(), tileBox.getCenterPixelX(), tileBox.getCenterPixelY()); String selectedPointId = getSelectedContextMenuPointId(); @@ -234,12 +243,8 @@ public class AidlMapLayer extends OsmandMapLayer implements IContextMenuProvider return Boolean.parseBoolean(point.getParams().get(AMapPoint.POINT_STALE_LOC_PARAM)); } - private boolean isAppEnabled() { - return map.getMyApplication().getAidlApi().isAppEnabled(packName); - } - - public String getPackName() { - return packName; + private boolean isLayerEnabled() { + return map.getMyApplication().getAidlApi().isAppEnabled(packName) && appLayersPref.get() && layerPref.get(); } @Override @@ -273,7 +278,7 @@ public class AidlMapLayer extends OsmandMapLayer implements IContextMenuProvider @Override public void collectObjectsFromPoint(PointF point, RotatedTileBox tileBox, List o, boolean unknownLocation) { - if (isAppEnabled()) { + if (isLayerEnabled()) { getFromPoint(tileBox, point, o); } } From e0dfa29d239b809e3b8af8897fb6c1e3319a447f Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 4 Dec 2019 15:43:43 +0200 Subject: [PATCH 4/6] Move layers and widgets to connected app and add configure map layers preference --- OsmAnd/src/net/osmand/AndroidUtils.java | 9 + OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 564 +++++++++++------- .../osmand/plus/OsmAndAppCustomization.java | 12 +- .../builders/cards/ImageCard.java | 12 +- .../controllers/AMapPointMenuController.java | 17 +- .../net/osmand/plus/views/AidlMapLayer.java | 4 +- 6 files changed, 382 insertions(+), 236 deletions(-) diff --git a/OsmAnd/src/net/osmand/AndroidUtils.java b/OsmAnd/src/net/osmand/AndroidUtils.java index e27d316644..ef52c769df 100644 --- a/OsmAnd/src/net/osmand/AndroidUtils.java +++ b/OsmAnd/src/net/osmand/AndroidUtils.java @@ -53,8 +53,10 @@ import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.TextView; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.download.DownloadActivity; +import net.osmand.util.Algorithms; import java.io.File; import java.util.Collections; @@ -437,6 +439,13 @@ public class AndroidUtils { return outValue.getFloat(); } + public static int getDrawableId(OsmandApplication app, String id) { + if (!Algorithms.isEmpty(id)) { + return app.getResources().getIdentifier(id, "drawable", app.getPackageName()); + } + return 0; + } + public static int getStatusBarHeight(Context ctx) { int result = 0; int resourceId = ctx.getResources().getIdentifier("status_bar_height", "dimen", "android"); diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 1ffff0bccc..198e3d5209 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -20,9 +20,11 @@ import android.os.ParcelFileDescriptor; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; -import android.text.TextUtils; import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.CompoundButton; +import net.osmand.AndroidUtils; import net.osmand.CallbackWithObject; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; @@ -43,6 +45,7 @@ import net.osmand.plus.AppInitializer.AppInitializeListener; import net.osmand.plus.AppInitializer.InitEvents; import net.osmand.plus.ApplicationMode; import net.osmand.plus.ContextMenuAdapter; +import net.osmand.plus.ContextMenuItem; import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.GPXDatabase.GpxDataItem; import net.osmand.plus.GpxSelectionHelper; @@ -53,6 +56,8 @@ import net.osmand.plus.OsmAndAppCustomization; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandSettings; +import net.osmand.plus.OsmandSettings.CommonPreference; +import net.osmand.plus.R; import net.osmand.plus.SQLiteTileSource; import net.osmand.plus.SettingsHelper; import net.osmand.plus.activities.MapActivity; @@ -184,10 +189,6 @@ public class OsmandAidlApi { private static final int DEFAULT_ZOOM = 15; private OsmandApplication app; - private Map widgets = new ConcurrentHashMap<>(); - private Map widgetControls = new ConcurrentHashMap<>(); - private Map layers = new ConcurrentHashMap<>(); - private Map mapLayers = new ConcurrentHashMap<>(); private Map receivers = new TreeMap<>(); private Map connectedApps = new ConcurrentHashMap<>(); private Map contextMenuButtonsParams = new ConcurrentHashMap<>(); @@ -307,14 +308,6 @@ public class OsmandAidlApi { registerReceiver(setMapLocationReceiver, mapActivity, AIDL_SET_MAP_LOCATION); } - private int getDrawableId(String id) { - if (Algorithms.isEmpty(id)) { - return 0; - } else { - return app.getResources().getIdentifier(id, "drawable", app.getPackageName()); - } - } - private void registerAddMapWidgetReceiver(MapActivity mapActivity) { final WeakReference mapActivityRef = new WeakReference<>(mapActivity); BroadcastReceiver addMapWidgetReceiver = new BroadcastReceiver() { @@ -322,14 +315,16 @@ public class OsmandAidlApi { public void onReceive(Context context, Intent intent) { MapActivity mapActivity = mapActivityRef.get(); String widgetId = intent.getStringExtra(AIDL_OBJECT_ID); - if (mapActivity != null && widgetId != null) { - AidlMapWidgetWrapper widget = widgets.get(widgetId); - if (widget != null) { + String packName = intent.getStringExtra(AIDL_PACKAGE_NAME); + if (mapActivity != null && widgetId != null && packName != null) { + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + AidlMapWidgetWrapper widget = connectedApp.widgets.get(widgetId); MapInfoLayer layer = mapActivity.getMapLayers().getMapInfoLayer(); - if (layer != null) { - TextInfoWidget control = createWidgetControl(mapActivity, widgetId); - widgetControls.put(widgetId, control); - int menuIconId = getDrawableId(widget.getMenuIconName()); + if (widget != null && layer != null) { + TextInfoWidget control = connectedApp.createWidgetControl(mapActivity, widgetId); + connectedApp.widgetControls.put(widgetId, control); + int menuIconId = AndroidUtils.getDrawableId(app, widget.getMenuIconName()); MapWidgetRegInfo widgetInfo = layer.registerSideWidget(control, menuIconId, widget.getMenuTitle(), "aidl_widget_" + widgetId, false, widget.getOrder()); @@ -378,13 +373,17 @@ public class OsmandAidlApi { public void onReceive(Context context, Intent intent) { MapActivity mapActivity = mapActivityRef.get(); String widgetId = intent.getStringExtra(AIDL_OBJECT_ID); - if (mapActivity != null && widgetId != null) { - MapInfoLayer layer = mapActivity.getMapLayers().getMapInfoLayer(); - TextInfoWidget widgetControl = widgetControls.get(widgetId); - if (layer != null && widgetControl != null) { - layer.removeSideWidget(widgetControl); - widgetControls.remove(widgetId); - layer.recreateControls(); + String packName = intent.getStringExtra(AIDL_PACKAGE_NAME); + if (mapActivity != null && widgetId != null && packName != null) { + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + MapInfoLayer layer = mapActivity.getMapLayers().getMapInfoLayer(); + TextInfoWidget widgetControl = connectedApp.widgetControls.get(widgetId); + if (layer != null && widgetControl != null) { + layer.removeSideWidget(widgetControl); + connectedApp.widgetControls.remove(widgetId); + layer.recreateControls(); + } } } } @@ -393,19 +392,8 @@ public class OsmandAidlApi { } public void registerWidgetControls(MapActivity mapActivity) { - for (AidlMapWidgetWrapper widget : widgets.values()) { - MapInfoLayer layer = mapActivity.getMapLayers().getMapInfoLayer(); - if (layer != null) { - TextInfoWidget control = createWidgetControl(mapActivity, widget.getId()); - widgetControls.put(widget.getId(), control); - int menuIconId = getDrawableId(widget.getMenuIconName()); - MapWidgetRegInfo widgetInfo = layer.registerSideWidget(control, - menuIconId, widget.getMenuTitle(), "aidl_widget_" + widget.getId(), - false, widget.getOrder()); - if (!mapActivity.getMapLayers().getMapWidgetRegistry().isVisible(widgetInfo.key)) { - mapActivity.getMapLayers().getMapWidgetRegistry().setVisibility(widgetInfo, true, false); - } - } + for (ConnectedApp connectedApp : connectedApps.values()) { + connectedApp.registerWidgetControls(mapActivity); } } @@ -417,16 +405,19 @@ public class OsmandAidlApi { MapActivity mapActivity = mapActivityRef.get(); String layerId = intent.getStringExtra(AIDL_OBJECT_ID); String packName = intent.getStringExtra(AIDL_PACKAGE_NAME); - if (mapActivity != null && packName != null && layerId != null) { - AidlMapLayerWrapper layer = layers.get(layerId); - if (layer != null) { - OsmandMapLayer mapLayer = mapLayers.get(layerId); - if (mapLayer != null) { - mapActivity.getMapView().removeLayer(mapLayer); + if (mapActivity != null && layerId != null && packName != null) { + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + AidlMapLayerWrapper layer = connectedApp.layers.get(layerId); + if (layer != null) { + OsmandMapLayer mapLayer = connectedApp.mapLayers.get(layerId); + if (mapLayer != null) { + mapActivity.getMapView().removeLayer(mapLayer); + } + mapLayer = new AidlMapLayer(mapActivity, layer, connectedApp.pack); + mapActivity.getMapView().addLayer(mapLayer, layer.getZOrder()); + connectedApp.mapLayers.put(layerId, mapLayer); } - mapLayer = new AidlMapLayer(mapActivity, layer, packName); - mapActivity.getMapView().addLayer(mapLayer, layer.getZOrder()); - mapLayers.put(layerId, mapLayer); } } } @@ -441,11 +432,15 @@ public class OsmandAidlApi { public void onReceive(Context context, Intent intent) { MapActivity mapActivity = mapActivityRef.get(); String layerId = intent.getStringExtra(AIDL_OBJECT_ID); - if (mapActivity != null && layerId != null) { - OsmandMapLayer mapLayer = mapLayers.remove(layerId); - if (mapLayer != null) { - mapActivity.getMapView().removeLayer(mapLayer); - mapActivity.refreshMap(); + String packName = intent.getStringExtra(AIDL_PACKAGE_NAME); + if (mapActivity != null && layerId != null && packName != null) { + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + OsmandMapLayer mapLayer = connectedApp.mapLayers.remove(layerId); + if (mapLayer != null) { + mapActivity.getMapView().removeLayer(mapLayer); + mapActivity.refreshMap(); + } } } } @@ -837,18 +832,8 @@ public class OsmandAidlApi { } public void registerMapLayers(MapActivity mapActivity) { - for (AidlMapLayerWrapper layer : layers.values()) { - OsmandMapLayer mapLayer = mapLayers.get(layer.getId()); - String packName = ""; - if (mapLayer != null) { - if (mapLayer instanceof AidlMapLayer) { - packName = ((AidlMapLayer) mapLayer).getPackName(); - } - mapActivity.getMapView().removeLayer(mapLayer); - } - mapLayer = new AidlMapLayer(mapActivity, layer, packName); - mapActivity.getMapView().addLayer(mapLayer, layer.getZOrder()); - mapLayers.put(layer.getId(), mapLayer); + for (ConnectedApp connectedApp : connectedApps.values()) { + connectedApp.registerMapLayers(mapActivity); } } @@ -858,43 +843,6 @@ public class OsmandAidlApi { app.sendBroadcast(intent); } - private TextInfoWidget createWidgetControl(MapActivity mapActivity, final String widgetId) { - final TextInfoWidget control = new TextInfoWidget(mapActivity) { - - @Override - public boolean updateInfo(DrawSettings drawSettings) { - AidlMapWidgetWrapper widget = widgets.get(widgetId); - if (widget != null) { - String txt = widget.getText(); - String subtxt = widget.getDescription(); - boolean night = drawSettings != null && drawSettings.isNightMode(); - int icon = night ? getDrawableId(widget.getDarkIconName()) : getDrawableId(widget.getLightIconName()); - setText(txt, subtxt); - if (icon != 0) { - setImageDrawable(icon); - } else { - setImageDrawable(null); - } - return true; - } else { - return false; - } - } - }; - control.updateInfo(null); - - control.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - AidlMapWidgetWrapper widget = widgets.get(widgetId); - if (widget != null && widget.getIntentOnClick() != null) { - app.startActivity(widget.getIntentOnClick()); - } - } - }); - return control; - } - boolean reloadMap() { refreshMap(); return true; @@ -1056,97 +1004,71 @@ public class OsmandAidlApi { return false; } - boolean addMapWidget(AidlMapWidgetWrapper widget) { + boolean addMapWidget(String packName, AidlMapWidgetWrapper widget) { if (widget != null) { - if (widgets.containsKey(widget.getId())) { - updateMapWidget(widget); - } else { - widgets.put(widget.getId(), widget); - Intent intent = new Intent(); - intent.setAction(AIDL_ADD_MAP_WIDGET); - intent.putExtra(AIDL_OBJECT_ID, widget.getId()); - app.sendBroadcast(intent); + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + return connectedApp.addMapWidget(widget); } - refreshMap(); - return true; - } else { - return false; } + return false; } - boolean removeMapWidget(String widgetId) { - if (!Algorithms.isEmpty(widgetId) && widgets.containsKey(widgetId)) { - widgets.remove(widgetId); - Intent intent = new Intent(); - intent.setAction(AIDL_REMOVE_MAP_WIDGET); - intent.putExtra(AIDL_OBJECT_ID, widgetId); - app.sendBroadcast(intent); - return true; - } else { - return false; + boolean removeMapWidget(String packName, String widgetId) { + if (!Algorithms.isEmpty(widgetId)) { + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + return connectedApp.removeMapWidget(widgetId); + } } + return false; } - boolean updateMapWidget(AidlMapWidgetWrapper widget) { - if (widget != null && widgets.containsKey(widget.getId())) { - widgets.put(widget.getId(), widget); - refreshMap(); - return true; - } else { - return false; + boolean updateMapWidget(String packName, AidlMapWidgetWrapper widget) { + if (widget != null) { + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + return connectedApp.updateMapWidget(widget); + } } + return false; } boolean addMapLayer(String packName, AidlMapLayerWrapper layer) { if (layer != null) { - if (layers.containsKey(layer.getId())) { - updateMapLayer(layer); - } else { - layers.put(layer.getId(), layer); - Intent intent = new Intent(); - intent.setAction(AIDL_ADD_MAP_LAYER); - intent.putExtra(AIDL_OBJECT_ID, layer.getId()); - intent.putExtra(AIDL_PACKAGE_NAME, packName); - app.sendBroadcast(intent); + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + return connectedApp.addMapLayer(layer); } - refreshMap(); - return true; - } else { - return false; } + return false; } - boolean removeMapLayer(String layerId) { - if (!Algorithms.isEmpty(layerId) && layers.containsKey(layerId)) { - layers.remove(layerId); - Intent intent = new Intent(); - intent.setAction(AIDL_REMOVE_MAP_LAYER); - intent.putExtra(AIDL_OBJECT_ID, layerId); - app.sendBroadcast(intent); - return true; - } else { - return false; - } - } - - boolean updateMapLayer(AidlMapLayerWrapper layer) { - if (layer != null && layers.containsKey(layer.getId())) { - AidlMapLayerWrapper existingLayer = layers.get(layer.getId()); - for (AidlMapPointWrapper point : layer.getPoints()) { - existingLayer.putPoint(point); + boolean removeMapLayer(String packName, String layerId) { + if (!Algorithms.isEmpty(layerId)) { + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + return connectedApp.removeMapLayer(layerId); } - existingLayer.copyZoomBounds(layer); - refreshMap(); - return true; - } else { - return false; } + return false; } - boolean showMapPoint(String layerId, AidlMapPointWrapper point) { + boolean updateMapLayer(String packName, AidlMapLayerWrapper layer) { + if (layer != null) { + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + return connectedApp.updateMapLayer(layer); + } + } + return false; + } + + boolean showMapPoint(String packName, String layerId, AidlMapPointWrapper point) { if (point != null) { - if (!TextUtils.isEmpty(layerId)) { - AidlMapLayerWrapper layer = layers.get(layerId); + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null && !Algorithms.isEmpty(layerId)) { + AidlMapLayerWrapper layer = connectedApp.layers.get(layerId); if (layer != null) { AidlMapPointWrapper p = layer.getPoint(point.getId()); if (p != null) { @@ -1169,40 +1091,31 @@ public class OsmandAidlApi { return false; } - boolean putMapPoint(String layerId, AidlMapPointWrapper point) { + boolean putMapPoint(String packName, String layerId, AidlMapPointWrapper point) { if (point != null) { - AidlMapLayerWrapper layer = layers.get(layerId); - if (layer != null) { - layer.putPoint(point); - refreshMap(); - return true; + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + return connectedApp.putMapPoint(layerId, point); } } return false; } - boolean updateMapPoint(String layerId, AidlMapPointWrapper point, boolean updateOpenedMenuAndMap) { + boolean updateMapPoint(String packName, String layerId, AidlMapPointWrapper point, boolean updateOpenedMenuAndMap) { if (point != null) { - AidlMapLayerWrapper layer = layers.get(layerId); - if (layer != null) { - layer.putPoint(point); - refreshMap(); - if (updateOpenedMenuAndMap && aMapPointUpdateListener != null) { - aMapPointUpdateListener.onAMapPointUpdated(point, layerId); - } - return true; + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + return connectedApp.updateMapPoint(layerId, point, updateOpenedMenuAndMap); } } return false; } - boolean removeMapPoint(String layerId, String pointId) { - if (pointId != null) { - AidlMapLayerWrapper layer = layers.get(layerId); - if (layer != null) { - layer.removePoint(pointId); - refreshMap(); - return true; + boolean removeMapPoint(String packName, String layerId, String pointId) { + if (layerId != null && pointId != null) { + ConnectedApp connectedApp = connectedApps.get(packName); + if (connectedApp != null) { + return connectedApp.removeMapPoint(layerId, pointId); } } return false; @@ -1838,13 +1751,13 @@ public class OsmandAidlApi { } public boolean isAppEnabled(@NonNull String pack) { - ConnectedApp app = connectedApps.get(pack); - if (app == null) { - app = new ConnectedApp(pack, true); - connectedApps.put(pack, app); + ConnectedApp connectedApp = connectedApps.get(pack); + if (connectedApp == null) { + connectedApp = new ConnectedApp(app, pack, true); + connectedApps.put(pack, connectedApp); saveConnectedApps(); } - return app.enabled; + return connectedApp.enabled; } private boolean saveConnectedApps() { @@ -1871,7 +1784,7 @@ public class OsmandAidlApi { JSONObject obj = array.getJSONObject(i); String pack = obj.optString(ConnectedApp.PACK_KEY, ""); boolean enabled = obj.optBoolean(ConnectedApp.ENABLED_KEY, true); - connectedApps.put(pack, new ConnectedApp(pack, enabled)); + connectedApps.put(pack, new ConnectedApp(app, pack, enabled)); } } catch (JSONException e) { e.printStackTrace(); @@ -2198,6 +2111,12 @@ public class OsmandAidlApi { return false; } + public void registerLayerContextMenu(OsmandMapTileView mapView, ContextMenuAdapter adapter, MapActivity mapActivity) { + for (ConnectedApp connectedApp : getConnectedApps()) { + connectedApp.registerLayerContextMenu(mapView, adapter, mapActivity); + } + } + private class FileCopyInfo { long startTime; long lastAccessTime; @@ -2339,17 +2258,34 @@ public class OsmandAidlApi { public static class ConnectedApp implements Comparable { - static final String PACK_KEY = "pack"; - static final String ENABLED_KEY = "enabled"; + public static final String LAYERS_PREFIX = "layers_"; + public static final String WIDGETS_PREFIX = "widgets_"; + + private static final String PACK_KEY = "pack"; + private static final String ENABLED_KEY = "enabled"; + + private OsmandApplication app; + + private Map widgets = new ConcurrentHashMap<>(); + private Map widgetControls = new ConcurrentHashMap<>(); + + private Map layers = new ConcurrentHashMap<>(); + private Map mapLayers = new ConcurrentHashMap<>(); + + private CommonPreference layersPref; private String pack; - private boolean enabled; private String name; + private Drawable icon; - ConnectedApp(String pack, boolean enabled) { + private boolean enabled; + + ConnectedApp(OsmandApplication app, String pack, boolean enabled) { + this.app = app; this.pack = pack; this.enabled = enabled; + layersPref = app.getSettings().registerBooleanPreference(LAYERS_PREFIX + pack, true).cache(); } public boolean isEnabled() { @@ -2368,6 +2304,226 @@ public class OsmandAidlApi { return icon; } + private void registerMapLayers(MapActivity mapActivity) { + for (AidlMapLayerWrapper layer : layers.values()) { + OsmandMapLayer mapLayer = mapLayers.get(layer.getId()); + if (mapLayer != null) { + mapActivity.getMapView().removeLayer(mapLayer); + } + mapLayer = new AidlMapLayer(mapActivity, layer, pack); + mapActivity.getMapView().addLayer(mapLayer, layer.getZOrder()); + mapLayers.put(layer.getId(), mapLayer); + } + } + + private void registerLayerContextMenu(OsmandMapTileView mapView, final ContextMenuAdapter menuAdapter, final MapActivity mapActivity) { + ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() { + + @Override + public boolean onRowItemClick(ArrayAdapter adapter, View view, int itemId, int position) { + CompoundButton btn = view.findViewById(R.id.toggle_item); + if (btn != null && btn.getVisibility() == View.VISIBLE) { + btn.setChecked(!btn.isChecked()); + menuAdapter.getItem(position).setColorRes(btn.isChecked() ? R.color.osmand_orange : ContextMenuItem.INVALID_ID); + adapter.notifyDataSetChanged(); + return false; + } + return true; + } + + @Override + public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, + int position, boolean isChecked, int[] viewCoordinates) { + if (layersPref.set(isChecked)) { + ContextMenuItem item = adapter.getItem(position); + if (item != null) { + item.setColorRes(isChecked ? R.color.osmand_orange : ContextMenuItem.INVALID_ID); + item.setSelected(isChecked); + adapter.notifyDataSetChanged(); + } + mapActivity.refreshMap(); + } + return false; + } + }; + boolean layersEnabled = layersPref.get(); + menuAdapter.addItem(new ContextMenuItem.ItemBuilder() + .setId(LAYERS_PREFIX + pack) + .setTitle(name) + .setListener(listener) + .setSelected(layersEnabled) + .setIcon(R.drawable.ic_extension_dark) + .setColor(layersEnabled ? R.color.osmand_orange : ContextMenuItem.INVALID_ID) + .createItem()); + } + + private void registerWidgetControls(MapActivity mapActivity) { + for (AidlMapWidgetWrapper widget : widgets.values()) { + MapInfoLayer layer = mapActivity.getMapLayers().getMapInfoLayer(); + if (layer != null) { + TextInfoWidget control = createWidgetControl(mapActivity, widget.getId()); + widgetControls.put(widget.getId(), control); + int menuIconId = AndroidUtils.getDrawableId(mapActivity.getMyApplication(), widget.getMenuIconName()); + MapWidgetRegInfo widgetInfo = layer.registerSideWidget(control, menuIconId, + widget.getMenuTitle(), "aidl_widget_" + widget.getId(), false, widget.getOrder()); + if (!mapActivity.getMapLayers().getMapWidgetRegistry().isVisible(widgetInfo.key)) { + mapActivity.getMapLayers().getMapWidgetRegistry().setVisibility(widgetInfo, true, false); + } + } + } + } + + private TextInfoWidget createWidgetControl(final MapActivity mapActivity, final String widgetId) { + TextInfoWidget control = new TextInfoWidget(mapActivity) { + @Override + public boolean updateInfo(DrawSettings drawSettings) { + AidlMapWidgetWrapper widget = widgets.get(widgetId); + if (widget != null) { + String txt = widget.getText(); + String subtxt = widget.getDescription(); + boolean night = drawSettings != null && drawSettings.isNightMode(); + int icon = AndroidUtils.getDrawableId(mapActivity.getMyApplication(), night ? widget.getDarkIconName() : widget.getLightIconName()); + setText(txt, subtxt); + if (icon != 0) { + setImageDrawable(icon); + } else { + setImageDrawable(null); + } + return true; + } + return false; + } + }; + control.updateInfo(null); + + control.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + AidlMapWidgetWrapper widget = widgets.get(widgetId); + if (widget != null && widget.getIntentOnClick() != null) { + app.startActivity(widget.getIntentOnClick()); + } + } + }); + return control; + } + + boolean addMapWidget(AidlMapWidgetWrapper widget) { + if (widget != null) { + if (widgets.containsKey(widget.getId())) { + updateMapWidget(widget); + } else { + widgets.put(widget.getId(), widget); + Intent intent = new Intent(); + intent.setAction(AIDL_ADD_MAP_WIDGET); + intent.putExtra(AIDL_OBJECT_ID, widget.getId()); + app.sendBroadcast(intent); + } + app.getAidlApi().refreshMap(); + return true; + } + return false; + } + + boolean removeMapWidget(String widgetId) { + if (!Algorithms.isEmpty(widgetId) && widgets.containsKey(widgetId)) { + widgets.remove(widgetId); + Intent intent = new Intent(); + intent.setAction(AIDL_REMOVE_MAP_WIDGET); + intent.putExtra(AIDL_OBJECT_ID, widgetId); + app.sendBroadcast(intent); + return true; + } + return false; + } + + boolean updateMapWidget(AidlMapWidgetWrapper widget) { + if (widget != null && widgets.containsKey(widget.getId())) { + widgets.put(widget.getId(), widget); + app.getAidlApi().refreshMap(); + return true; + } + return false; + } + + boolean addMapLayer(AidlMapLayerWrapper layer) { + if (layer != null) { + if (layers.containsKey(layer.getId())) { + updateMapLayer(layer); + } else { + layers.put(layer.getId(), layer); + Intent intent = new Intent(); + intent.setAction(AIDL_ADD_MAP_LAYER); + intent.putExtra(AIDL_OBJECT_ID, layer.getId()); + intent.putExtra(AIDL_PACKAGE_NAME, pack); + app.sendBroadcast(intent); + } + app.getAidlApi().refreshMap(); + return true; + } + return false; + } + + boolean removeMapLayer(String layerId) { + if (!Algorithms.isEmpty(layerId) && layers.containsKey(layerId)) { + layers.remove(layerId); + Intent intent = new Intent(); + intent.setAction(AIDL_REMOVE_MAP_LAYER); + intent.putExtra(AIDL_OBJECT_ID, layerId); + app.sendBroadcast(intent); + return true; + } + return false; + } + + boolean updateMapLayer(AidlMapLayerWrapper layer) { + if (layer != null) { + AidlMapLayerWrapper existingLayer = layers.get(layer.getId()); + if (existingLayer != null) { + for (AidlMapPointWrapper point : layer.getPoints()) { + existingLayer.putPoint(point); + } + existingLayer.copyZoomBounds(layer); + app.getAidlApi().refreshMap(); + return true; + } + } + return false; + } + + boolean putMapPoint(String layerId, AidlMapPointWrapper point) { + AidlMapLayerWrapper layer = layers.get(layerId); + if (layer != null) { + layer.putPoint(point); + app.getAidlApi().refreshMap(); + return true; + } + return false; + } + + boolean updateMapPoint(String layerId, AidlMapPointWrapper point, boolean updateOpenedMenuAndMap) { + AidlMapLayerWrapper layer = layers.get(layerId); + if (layer != null) { + layer.putPoint(point); + app.getAidlApi().refreshMap(); + if (updateOpenedMenuAndMap && app.getAidlApi().aMapPointUpdateListener != null) { + app.getAidlApi().aMapPointUpdateListener.onAMapPointUpdated(point, layerId); + } + return true; + } + return false; + } + + boolean removeMapPoint(String layerId, String pointId) { + AidlMapLayerWrapper layer = layers.get(layerId); + if (layer != null) { + layer.removePoint(pointId); + app.getAidlApi().refreshMap(); + return true; + } + return false; + } + @Override public int compareTo(@NonNull ConnectedApp app) { if (name != null && app.name != null) { diff --git a/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java b/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java index ea97bf83d2..9e438aefb7 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java @@ -13,6 +13,7 @@ import android.support.annotation.Nullable; import android.text.TextUtils; import android.widget.ArrayAdapter; +import net.osmand.AndroidUtils; import net.osmand.IProgress; import net.osmand.IndexConstants; import net.osmand.aidl.OsmandAidlApi; @@ -409,10 +410,11 @@ public class OsmAndAppCustomization { intent.addFlags(item.flags); } final Intent finalIntent = intent; + int iconId = AndroidUtils.getDrawableId(app, item.iconName); adapter.addItem(new ContextMenuItem.ItemBuilder() .setId(item.getId()) .setTitle(item.name) - .setIcon(getIconId(item.iconName)) + .setIcon(iconId != 0 ? iconId : ContextMenuItem.INVALID_ID) .setListener(new ContextMenuAdapter.ItemClickListener() { @Override public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, int position, boolean isChecked, int[] viewCoordinates) { @@ -454,14 +456,6 @@ public class OsmAndAppCustomization { return res; } - private int getIconId(@Nullable String iconName) { - if (!TextUtils.isEmpty(iconName)) { - int id = app.getResources().getIdentifier(iconName, "drawable", app.getPackageName()); - return id == 0 ? -1 : id; - } - return -1; - } - @NonNull private HashSet getAppModesSet(@Nullable List appModeKeys) { HashSet set = new HashSet<>(); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java index d632dc60d6..693d88fe44 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/cards/ImageCard.java @@ -134,10 +134,10 @@ public abstract class ImageCard extends AbstractCard { this.externalLink = imageObject.getBoolean("externalLink"); } if (imageObject.has("topIcon") && !imageObject.isNull("topIcon")) { - this.topIconId = getDrawableId(imageObject.getString("topIcon")); + this.topIconId = AndroidUtils.getDrawableId(getMyApplication(), imageObject.getString("topIcon")); } if (imageObject.has("buttonIcon") && !imageObject.isNull("buttonIcon")) { - this.buttonIconId = getDrawableId(imageObject.getString("buttonIcon")); + this.buttonIconId = AndroidUtils.getDrawableId(getMyApplication(), imageObject.getString("buttonIcon")); } if (imageObject.has("buttonText") && !imageObject.isNull("buttonText")) { this.buttonText = imageObject.getString("buttonText"); @@ -169,14 +169,6 @@ public abstract class ImageCard extends AbstractCard { } } - private int getDrawableId(String id) { - if (Algorithms.isEmpty(id)) { - return 0; - } else { - return getMyApplication().getResources().getIdentifier(id, "drawable", getMyApplication().getPackageName()); - } - } - private static ImageCard createCard(MapActivity mapActivity, JSONObject imageObject) { ImageCard imageCard = null; try { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AMapPointMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AMapPointMenuController.java index f4e3be4253..e59168a5c2 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AMapPointMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AMapPointMenuController.java @@ -10,6 +10,7 @@ import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.Pair; +import net.osmand.AndroidUtils; import net.osmand.aidl.AidlContextMenuButtonWrapper; import net.osmand.aidl.AidlContextMenuButtonsWrapper; import net.osmand.aidl.AidlMapPointWrapper; @@ -196,10 +197,11 @@ public class AMapPointMenuController extends MenuController { } } }; + OsmandApplication app = mapActivity.getMyApplication(); titleButtonController.caption = contextMenuButton.getLeftTextCaption(); titleButtonController.rightTextCaption = contextMenuButton.getRightTextCaption(); - titleButtonController.leftIconId = getIconIdByName(contextMenuButton.getLeftIconName()); - titleButtonController.rightIconId = getIconIdByName(contextMenuButton.getRightIconName()); + titleButtonController.leftIconId = AndroidUtils.getDrawableId(app, contextMenuButton.getLeftIconName()); + titleButtonController.rightIconId = AndroidUtils.getDrawableId(app, contextMenuButton.getRightIconName()); titleButtonController.enabled = contextMenuButton.isEnabled(); titleButtonController.tintIcon = contextMenuButton.isTintIcon(); @@ -211,7 +213,7 @@ public class AMapPointMenuController extends MenuController { if (activity != null) { String iconName = point.getParams().get(AMapPoint.POINT_TYPE_ICON_NAME_PARAM); if (!TextUtils.isEmpty(iconName)) { - return getIconIdByName(iconName); + return AndroidUtils.getDrawableId(activity.getMyApplication(), iconName); } } if (!TextUtils.isEmpty(point.getShortName())) { @@ -220,15 +222,6 @@ public class AMapPointMenuController extends MenuController { return NO_ICON; } - private int getIconIdByName(String iconName) { - MapActivity activity = getMapActivity(); - if (activity != null && !TextUtils.isEmpty(iconName)) { - OsmandApplication app = activity.getMyApplication(); - return app.getResources().getIdentifier(iconName, "drawable", app.getPackageName()); - } - return 0; - } - private float getPointSpeed() { String speed = point.getParams().get(AMapPoint.POINT_SPEED_PARAM); if (!TextUtils.isEmpty(speed)) { diff --git a/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java b/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java index 51f86b56ec..218764da93 100644 --- a/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java @@ -41,6 +41,8 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import static net.osmand.aidl.OsmandAidlApi.ConnectedApp.LAYERS_PREFIX; + public class AidlMapLayer extends OsmandMapLayer implements IContextMenuProvider, MapTextLayer.MapTextProvider { private static final float POINT_IMAGE_VERTICAL_OFFSET = 0.91f; @@ -90,7 +92,7 @@ public class AidlMapLayer extends OsmandMapLayer implements IContextMenuProvider OsmandApplication app = map.getMyApplication(); layerPref = app.getSettings().registerBooleanPreference(packName + "_" + aidlLayer.getId(), true); - appLayersPref = app.getSettings().registerBooleanPreference("layers_" + packName, true); + appLayersPref = app.getSettings().registerBooleanPreference(LAYERS_PREFIX + packName, true); } @Override From bc6ba2ab3552c125b0ade2f7325a216bf3d42461 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 4 Dec 2019 17:19:48 +0200 Subject: [PATCH 5/6] Move ConnectedApp outside OsmandAidlApi --- OsmAnd/src/net/osmand/aidl/ConnectedApp.java | 354 ++++++++++++++++++ OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 347 ++--------------- .../osmand/plus/OsmAndAppCustomization.java | 4 +- .../plus/activities/PluginsActivity.java | 2 +- .../osmand/plus/dialogs/ConfigureMapMenu.java | 2 +- .../settings/ConfigureProfileFragment.java | 2 +- .../net/osmand/plus/views/AidlMapLayer.java | 4 +- 7 files changed, 395 insertions(+), 320 deletions(-) create mode 100644 OsmAnd/src/net/osmand/aidl/ConnectedApp.java diff --git a/OsmAnd/src/net/osmand/aidl/ConnectedApp.java b/OsmAnd/src/net/osmand/aidl/ConnectedApp.java new file mode 100644 index 0000000000..a44910100a --- /dev/null +++ b/OsmAnd/src/net/osmand/aidl/ConnectedApp.java @@ -0,0 +1,354 @@ +package net.osmand.aidl; + +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.graphics.drawable.Drawable; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.CompoundButton; + +import net.osmand.AndroidUtils; +import net.osmand.plus.ContextMenuAdapter; +import net.osmand.plus.ContextMenuItem; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings; +import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.views.AidlMapLayer; +import net.osmand.plus.views.MapInfoLayer; +import net.osmand.plus.views.OsmandMapLayer; +import net.osmand.plus.views.mapwidgets.MapWidgetRegistry; +import net.osmand.plus.views.mapwidgets.TextInfoWidget; +import net.osmand.util.Algorithms; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + + +public class ConnectedApp implements Comparable { + + public static final String AIDL_LAYERS_PREFIX = "aidl_layers_"; + public static final String AIDL_WIDGETS_PREFIX = "aidl_widgets_"; + + static final String AIDL_OBJECT_ID = "aidl_object_id"; + static final String AIDL_PACKAGE_NAME = "aidl_package_name"; + + static final String AIDL_ADD_MAP_WIDGET = "aidl_add_map_widget"; + static final String AIDL_REMOVE_MAP_WIDGET = "aidl_remove_map_widget"; + + static final String AIDL_ADD_MAP_LAYER = "aidl_add_map_layer"; + static final String AIDL_REMOVE_MAP_LAYER = "aidl_remove_map_layer"; + + static final String PACK_KEY = "pack"; + static final String ENABLED_KEY = "enabled"; + + private OsmandApplication app; + + private Map widgets = new ConcurrentHashMap<>(); + private Map widgetControls = new ConcurrentHashMap<>(); + + private Map layers = new ConcurrentHashMap<>(); + private Map mapLayers = new ConcurrentHashMap<>(); + + private OsmandSettings.CommonPreference layersPref; + + private String pack; + private String name; + + private Drawable icon; + + private boolean enabled; + + ConnectedApp(OsmandApplication app, String pack, boolean enabled) { + this.app = app; + this.pack = pack; + this.enabled = enabled; + layersPref = app.getSettings().registerBooleanPreference(AIDL_LAYERS_PREFIX + pack, true).cache(); + } + + public boolean isEnabled() { + return enabled; + } + + @Nullable + public String getName() { + return name; + } + + @NonNull + public String getPack() { + return pack; + } + + @Nullable + public Drawable getIcon() { + return icon; + } + + @NonNull + public Map getWidgets() { + return widgets; + } + + @NonNull + public Map getWidgetControls() { + return widgetControls; + } + + @NonNull + public Map getLayers() { + return layers; + } + + @NonNull + public Map getMapLayers() { + return mapLayers; + } + + void switchEnabled() { + enabled = !enabled; + } + + void registerMapLayers(@NonNull MapActivity mapActivity) { + for (AidlMapLayerWrapper layer : layers.values()) { + OsmandMapLayer mapLayer = mapLayers.get(layer.getId()); + if (mapLayer != null) { + mapActivity.getMapView().removeLayer(mapLayer); + } + mapLayer = new AidlMapLayer(mapActivity, layer, pack); + mapActivity.getMapView().addLayer(mapLayer, layer.getZOrder()); + mapLayers.put(layer.getId(), mapLayer); + } + } + + void registerLayerContextMenu(final ContextMenuAdapter menuAdapter, final MapActivity mapActivity) { + ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() { + + @Override + public boolean onRowItemClick(ArrayAdapter adapter, View view, int itemId, int position) { + CompoundButton btn = view.findViewById(R.id.toggle_item); + if (btn != null && btn.getVisibility() == View.VISIBLE) { + btn.setChecked(!btn.isChecked()); + menuAdapter.getItem(position).setColorRes(btn.isChecked() ? R.color.osmand_orange : ContextMenuItem.INVALID_ID); + adapter.notifyDataSetChanged(); + return false; + } + return true; + } + + @Override + public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, + int position, boolean isChecked, int[] viewCoordinates) { + if (layersPref.set(isChecked)) { + ContextMenuItem item = adapter.getItem(position); + if (item != null) { + item.setColorRes(isChecked ? R.color.osmand_orange : ContextMenuItem.INVALID_ID); + item.setSelected(isChecked); + adapter.notifyDataSetChanged(); + } + mapActivity.refreshMap(); + } + return false; + } + }; + boolean layersEnabled = layersPref.get(); + menuAdapter.addItem(new ContextMenuItem.ItemBuilder() + .setId(AIDL_LAYERS_PREFIX + pack) + .setTitle(name) + .setListener(listener) + .setSelected(layersEnabled) + .setIcon(R.drawable.ic_extension_dark) + .setColor(layersEnabled ? R.color.osmand_orange : ContextMenuItem.INVALID_ID) + .createItem()); + } + + void registerWidgetControls(MapActivity mapActivity) { + for (AidlMapWidgetWrapper widget : widgets.values()) { + MapInfoLayer layer = mapActivity.getMapLayers().getMapInfoLayer(); + if (layer != null) { + TextInfoWidget control = createWidgetControl(mapActivity, widget.getId()); + widgetControls.put(widget.getId(), control); + int menuIconId = AndroidUtils.getDrawableId(mapActivity.getMyApplication(), widget.getMenuIconName()); + MapWidgetRegistry.MapWidgetRegInfo widgetInfo = layer.registerSideWidget(control, menuIconId, + widget.getMenuTitle(), "aidl_widget_" + widget.getId(), false, widget.getOrder()); + if (!mapActivity.getMapLayers().getMapWidgetRegistry().isVisible(widgetInfo.key)) { + mapActivity.getMapLayers().getMapWidgetRegistry().setVisibility(widgetInfo, true, false); + } + } + } + } + + TextInfoWidget createWidgetControl(final MapActivity mapActivity, final String widgetId) { + TextInfoWidget control = new TextInfoWidget(mapActivity) { + @Override + public boolean updateInfo(OsmandMapLayer.DrawSettings drawSettings) { + AidlMapWidgetWrapper widget = widgets.get(widgetId); + if (widget != null) { + String txt = widget.getText(); + String subtext = widget.getDescription(); + boolean night = drawSettings != null && drawSettings.isNightMode(); + int icon = AndroidUtils.getDrawableId(mapActivity.getMyApplication(), night ? widget.getDarkIconName() : widget.getLightIconName()); + setText(txt, subtext); + if (icon != 0) { + setImageDrawable(icon); + } else { + setImageDrawable(null); + } + return true; + } + return false; + } + }; + control.updateInfo(null); + + control.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + AidlMapWidgetWrapper widget = widgets.get(widgetId); + if (widget != null && widget.getIntentOnClick() != null) { + app.startActivity(widget.getIntentOnClick()); + } + } + }); + return control; + } + + boolean addMapWidget(AidlMapWidgetWrapper widget) { + if (widget != null) { + if (widgets.containsKey(widget.getId())) { + updateMapWidget(widget); + } else { + widgets.put(widget.getId(), widget); + Intent intent = new Intent(); + intent.setAction(AIDL_ADD_MAP_WIDGET); + intent.putExtra(AIDL_OBJECT_ID, widget.getId()); + app.sendBroadcast(intent); + } + app.getAidlApi().reloadMap(); + return true; + } + return false; + } + + boolean removeMapWidget(String widgetId) { + if (!Algorithms.isEmpty(widgetId) && widgets.containsKey(widgetId)) { + widgets.remove(widgetId); + Intent intent = new Intent(); + intent.setAction(AIDL_REMOVE_MAP_WIDGET); + intent.putExtra(AIDL_OBJECT_ID, widgetId); + app.sendBroadcast(intent); + return true; + } + return false; + } + + boolean updateMapWidget(AidlMapWidgetWrapper widget) { + if (widget != null && widgets.containsKey(widget.getId())) { + widgets.put(widget.getId(), widget); + app.getAidlApi().reloadMap(); + return true; + } + return false; + } + + boolean addMapLayer(AidlMapLayerWrapper layer) { + if (layer != null) { + if (layers.containsKey(layer.getId())) { + updateMapLayer(layer); + } else { + layers.put(layer.getId(), layer); + Intent intent = new Intent(); + intent.setAction(AIDL_ADD_MAP_LAYER); + intent.putExtra(AIDL_OBJECT_ID, layer.getId()); + intent.putExtra(AIDL_PACKAGE_NAME, pack); + app.sendBroadcast(intent); + } + app.getAidlApi().reloadMap(); + return true; + } + return false; + } + + boolean removeMapLayer(String layerId) { + if (!Algorithms.isEmpty(layerId) && layers.containsKey(layerId)) { + layers.remove(layerId); + Intent intent = new Intent(); + intent.setAction(AIDL_REMOVE_MAP_LAYER); + intent.putExtra(AIDL_OBJECT_ID, layerId); + app.sendBroadcast(intent); + return true; + } + return false; + } + + boolean updateMapLayer(AidlMapLayerWrapper layer) { + if (layer != null) { + AidlMapLayerWrapper existingLayer = layers.get(layer.getId()); + if (existingLayer != null) { + for (AidlMapPointWrapper point : layer.getPoints()) { + existingLayer.putPoint(point); + } + existingLayer.copyZoomBounds(layer); + app.getAidlApi().reloadMap(); + return true; + } + } + return false; + } + + boolean putMapPoint(String layerId, AidlMapPointWrapper point) { + AidlMapLayerWrapper layer = layers.get(layerId); + if (layer != null) { + layer.putPoint(point); + app.getAidlApi().reloadMap(); + return true; + } + return false; + } + + boolean updateMapPoint(String layerId, AidlMapPointWrapper point, boolean updateOpenedMenuAndMap) { + AidlMapLayerWrapper layer = layers.get(layerId); + if (layer != null) { + layer.putPoint(point); + app.getAidlApi().reloadMap(); + if (updateOpenedMenuAndMap && app.getAidlApi().getAMapPointUpdateListener() != null) { + app.getAidlApi().getAMapPointUpdateListener().onAMapPointUpdated(point, layerId); + } + return true; + } + return false; + } + + boolean removeMapPoint(String layerId, String pointId) { + AidlMapLayerWrapper layer = layers.get(layerId); + if (layer != null) { + layer.removePoint(pointId); + app.getAidlApi().reloadMap(); + return true; + } + return false; + } + + boolean updateApplicationInfo(PackageManager pm) { + try { + ApplicationInfo ai = pm.getPackageInfo(pack, 0).applicationInfo; + name = ai.loadLabel(pm).toString(); + icon = ai.loadIcon(pm); + return true; + } catch (PackageManager.NameNotFoundException e) { + // ignore + } + return false; + } + + @Override + public int compareTo(@NonNull ConnectedApp app) { + if (name != null && app.name != null) { + return name.compareTo(app.name); + } + return 0; + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 198e3d5209..637b2ed42c 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -106,6 +106,13 @@ import java.util.Map; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; +import static net.osmand.aidl.ConnectedApp.AIDL_OBJECT_ID; +import static net.osmand.aidl.ConnectedApp.AIDL_PACKAGE_NAME; +import static net.osmand.aidl.ConnectedApp.AIDL_ADD_MAP_LAYER; +import static net.osmand.aidl.ConnectedApp.AIDL_ADD_MAP_WIDGET; +import static net.osmand.aidl.ConnectedApp.AIDL_REMOVE_MAP_LAYER; +import static net.osmand.aidl.ConnectedApp.AIDL_REMOVE_MAP_WIDGET; + import static net.osmand.aidlapi.OsmandAidlConstants.COPY_FILE_IO_ERROR; import static net.osmand.aidlapi.OsmandAidlConstants.COPY_FILE_MAX_LOCK_TIME_MS; import static net.osmand.aidlapi.OsmandAidlConstants.COPY_FILE_PARAMS_ERROR; @@ -126,6 +133,7 @@ public class OsmandAidlApi { public static final int KEY_ON_VOICE_MESSAGE = 5; private static final Log LOG = PlatformUtil.getLog(OsmandAidlApi.class); + private static final String AIDL_REFRESH_MAP = "aidl_refresh_map"; private static final String AIDL_SET_MAP_LOCATION = "aidl_set_map_location"; private static final String AIDL_LATITUDE = "aidl_latitude"; @@ -147,18 +155,9 @@ public class OsmandAidlApi { private static final String AIDL_SEARCH_LAT = "aidl_search_lat"; private static final String AIDL_SEARCH_LON = "aidl_search_lon"; - private static final String AIDL_OBJECT_ID = "aidl_object_id"; - private static final String AIDL_PACKAGE_NAME = "aidl_package_name"; - - private static final String AIDL_ADD_MAP_WIDGET = "aidl_add_map_widget"; - private static final String AIDL_REMOVE_MAP_WIDGET = "aidl_remove_map_widget"; - private static final String AIDL_ADD_CONTEXT_MENU_BUTTONS = "aidl_add_context_menu_buttons"; private static final String AIDL_REMOVE_CONTEXT_MENU_BUTTONS = "aidl_remove_context_menu_buttons"; - private static final String AIDL_ADD_MAP_LAYER = "aidl_add_map_layer"; - private static final String AIDL_REMOVE_MAP_LAYER = "aidl_remove_map_layer"; - private static final String AIDL_TAKE_PHOTO_NOTE = "aidl_take_photo_note"; private static final String AIDL_START_VIDEO_RECORDING = "aidl_start_video_recording"; private static final String AIDL_START_AUDIO_RECORDING = "aidl_start_audio_recording"; @@ -252,6 +251,10 @@ public class OsmandAidlApi { return mapActivityActive; } + AMapPointUpdateListener getAMapPointUpdateListener() { + return aMapPointUpdateListener; + } + private void initOsmandTelegram() { String[] packages = new String[] {"net.osmand.telegram", "net.osmand.telegram.debug"}; Intent intent = new Intent("net.osmand.telegram.InitApp"); @@ -319,11 +322,11 @@ public class OsmandAidlApi { if (mapActivity != null && widgetId != null && packName != null) { ConnectedApp connectedApp = connectedApps.get(packName); if (connectedApp != null) { - AidlMapWidgetWrapper widget = connectedApp.widgets.get(widgetId); + AidlMapWidgetWrapper widget = connectedApp.getWidgets().get(widgetId); MapInfoLayer layer = mapActivity.getMapLayers().getMapInfoLayer(); if (widget != null && layer != null) { TextInfoWidget control = connectedApp.createWidgetControl(mapActivity, widgetId); - connectedApp.widgetControls.put(widgetId, control); + connectedApp.getWidgetControls().put(widgetId, control); int menuIconId = AndroidUtils.getDrawableId(app, widget.getMenuIconName()); MapWidgetRegInfo widgetInfo = layer.registerSideWidget(control, menuIconId, widget.getMenuTitle(), "aidl_widget_" + widgetId, @@ -378,10 +381,10 @@ public class OsmandAidlApi { ConnectedApp connectedApp = connectedApps.get(packName); if (connectedApp != null) { MapInfoLayer layer = mapActivity.getMapLayers().getMapInfoLayer(); - TextInfoWidget widgetControl = connectedApp.widgetControls.get(widgetId); + TextInfoWidget widgetControl = connectedApp.getWidgetControls().get(widgetId); if (layer != null && widgetControl != null) { layer.removeSideWidget(widgetControl); - connectedApp.widgetControls.remove(widgetId); + connectedApp.getWidgetControls().remove(widgetId); layer.recreateControls(); } } @@ -408,15 +411,15 @@ public class OsmandAidlApi { if (mapActivity != null && layerId != null && packName != null) { ConnectedApp connectedApp = connectedApps.get(packName); if (connectedApp != null) { - AidlMapLayerWrapper layer = connectedApp.layers.get(layerId); + AidlMapLayerWrapper layer = connectedApp.getLayers().get(layerId); if (layer != null) { - OsmandMapLayer mapLayer = connectedApp.mapLayers.get(layerId); + OsmandMapLayer mapLayer = connectedApp.getMapLayers().get(layerId); if (mapLayer != null) { mapActivity.getMapView().removeLayer(mapLayer); } - mapLayer = new AidlMapLayer(mapActivity, layer, connectedApp.pack); + mapLayer = new AidlMapLayer(mapActivity, layer, connectedApp.getPack()); mapActivity.getMapView().addLayer(mapLayer, layer.getZOrder()); - connectedApp.mapLayers.put(layerId, mapLayer); + connectedApp.getMapLayers().put(layerId, mapLayer); } } } @@ -436,7 +439,7 @@ public class OsmandAidlApi { if (mapActivity != null && layerId != null && packName != null) { ConnectedApp connectedApp = connectedApps.get(packName); if (connectedApp != null) { - OsmandMapLayer mapLayer = connectedApp.mapLayers.remove(layerId); + OsmandMapLayer mapLayer = connectedApp.getMapLayers().remove(layerId); if (mapLayer != null) { mapActivity.getMapView().removeLayer(mapLayer); mapActivity.refreshMap(); @@ -831,7 +834,7 @@ public class OsmandAidlApi { registerReceiver(hideSqliteDbFileReceiver, mapActivity, AIDL_HIDE_SQLITEDB_FILE); } - public void registerMapLayers(MapActivity mapActivity) { + public void registerMapLayers(@NonNull MapActivity mapActivity) { for (ConnectedApp connectedApp : connectedApps.values()) { connectedApp.registerMapLayers(mapActivity); } @@ -1068,7 +1071,7 @@ public class OsmandAidlApi { if (point != null) { ConnectedApp connectedApp = connectedApps.get(packName); if (connectedApp != null && !Algorithms.isEmpty(layerId)) { - AidlMapLayerWrapper layer = connectedApp.layers.get(layerId); + AidlMapLayerWrapper layer = connectedApp.getLayers().get(layerId); if (layer != null) { AidlMapPointWrapper p = layer.getPoint(point.getId()); if (p != null) { @@ -1720,9 +1723,9 @@ public class OsmandAidlApi { public ConnectedApp getConnectedApp(@NonNull String pack) { List connectedApps = getConnectedApps(); - for (ConnectedApp app : connectedApps) { - if (app.pack.equals(pack)) { - return app; + for (ConnectedApp connectedApp : connectedApps) { + if (connectedApp.getPack().equals(pack)) { + return connectedApp; } } return null; @@ -1732,13 +1735,8 @@ public class OsmandAidlApi { List res = new ArrayList<>(connectedApps.size()); PackageManager pm = app.getPackageManager(); for (ConnectedApp app : connectedApps.values()) { - try { - ApplicationInfo ai = pm.getPackageInfo(app.pack, 0).applicationInfo; - app.name = ai.loadLabel(pm).toString(); - app.icon = ai.loadIcon(pm); + if (app.updateApplicationInfo(pm)) { res.add(app); - } catch (PackageManager.NameNotFoundException e) { - // ignore } } Collections.sort(res); @@ -1746,7 +1744,7 @@ public class OsmandAidlApi { } public boolean switchEnabled(@NonNull ConnectedApp connectedApp) { - connectedApp.enabled = !connectedApp.enabled; + connectedApp.switchEnabled(); return saveConnectedApps(); } @@ -1757,16 +1755,16 @@ public class OsmandAidlApi { connectedApps.put(pack, connectedApp); saveConnectedApps(); } - return connectedApp.enabled; + return connectedApp.isEnabled(); } private boolean saveConnectedApps() { try { JSONArray array = new JSONArray(); - for (ConnectedApp app : connectedApps.values()) { + for (ConnectedApp connectedApp : connectedApps.values()) { JSONObject obj = new JSONObject(); - obj.put(ConnectedApp.ENABLED_KEY, app.enabled); - obj.put(ConnectedApp.PACK_KEY, app.pack); + obj.put(ConnectedApp.ENABLED_KEY, connectedApp.isEnabled()); + obj.put(ConnectedApp.PACK_KEY, connectedApp.getPack()); array.put(obj); } return app.getSettings().API_CONNECTED_APPS_JSON.set(array.toString()); @@ -2111,9 +2109,9 @@ public class OsmandAidlApi { return false; } - public void registerLayerContextMenu(OsmandMapTileView mapView, ContextMenuAdapter adapter, MapActivity mapActivity) { + public void registerLayerContextMenu(ContextMenuAdapter adapter, MapActivity mapActivity) { for (ConnectedApp connectedApp : getConnectedApps()) { - connectedApp.registerLayerContextMenu(mapView, adapter, mapActivity); + connectedApp.registerLayerContextMenu(adapter, mapActivity); } } @@ -2256,283 +2254,6 @@ public class OsmandAidlApi { a.points, a.wptPoints, a.wptCategoryNames); } - public static class ConnectedApp implements Comparable { - - public static final String LAYERS_PREFIX = "layers_"; - public static final String WIDGETS_PREFIX = "widgets_"; - - private static final String PACK_KEY = "pack"; - private static final String ENABLED_KEY = "enabled"; - - private OsmandApplication app; - - private Map widgets = new ConcurrentHashMap<>(); - private Map widgetControls = new ConcurrentHashMap<>(); - - private Map layers = new ConcurrentHashMap<>(); - private Map mapLayers = new ConcurrentHashMap<>(); - - private CommonPreference layersPref; - - private String pack; - private String name; - - private Drawable icon; - - private boolean enabled; - - ConnectedApp(OsmandApplication app, String pack, boolean enabled) { - this.app = app; - this.pack = pack; - this.enabled = enabled; - layersPref = app.getSettings().registerBooleanPreference(LAYERS_PREFIX + pack, true).cache(); - } - - public boolean isEnabled() { - return enabled; - } - - public String getName() { - return name; - } - - public String getPack() { - return pack; - } - - public Drawable getIcon() { - return icon; - } - - private void registerMapLayers(MapActivity mapActivity) { - for (AidlMapLayerWrapper layer : layers.values()) { - OsmandMapLayer mapLayer = mapLayers.get(layer.getId()); - if (mapLayer != null) { - mapActivity.getMapView().removeLayer(mapLayer); - } - mapLayer = new AidlMapLayer(mapActivity, layer, pack); - mapActivity.getMapView().addLayer(mapLayer, layer.getZOrder()); - mapLayers.put(layer.getId(), mapLayer); - } - } - - private void registerLayerContextMenu(OsmandMapTileView mapView, final ContextMenuAdapter menuAdapter, final MapActivity mapActivity) { - ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() { - - @Override - public boolean onRowItemClick(ArrayAdapter adapter, View view, int itemId, int position) { - CompoundButton btn = view.findViewById(R.id.toggle_item); - if (btn != null && btn.getVisibility() == View.VISIBLE) { - btn.setChecked(!btn.isChecked()); - menuAdapter.getItem(position).setColorRes(btn.isChecked() ? R.color.osmand_orange : ContextMenuItem.INVALID_ID); - adapter.notifyDataSetChanged(); - return false; - } - return true; - } - - @Override - public boolean onContextMenuClick(ArrayAdapter adapter, int itemId, - int position, boolean isChecked, int[] viewCoordinates) { - if (layersPref.set(isChecked)) { - ContextMenuItem item = adapter.getItem(position); - if (item != null) { - item.setColorRes(isChecked ? R.color.osmand_orange : ContextMenuItem.INVALID_ID); - item.setSelected(isChecked); - adapter.notifyDataSetChanged(); - } - mapActivity.refreshMap(); - } - return false; - } - }; - boolean layersEnabled = layersPref.get(); - menuAdapter.addItem(new ContextMenuItem.ItemBuilder() - .setId(LAYERS_PREFIX + pack) - .setTitle(name) - .setListener(listener) - .setSelected(layersEnabled) - .setIcon(R.drawable.ic_extension_dark) - .setColor(layersEnabled ? R.color.osmand_orange : ContextMenuItem.INVALID_ID) - .createItem()); - } - - private void registerWidgetControls(MapActivity mapActivity) { - for (AidlMapWidgetWrapper widget : widgets.values()) { - MapInfoLayer layer = mapActivity.getMapLayers().getMapInfoLayer(); - if (layer != null) { - TextInfoWidget control = createWidgetControl(mapActivity, widget.getId()); - widgetControls.put(widget.getId(), control); - int menuIconId = AndroidUtils.getDrawableId(mapActivity.getMyApplication(), widget.getMenuIconName()); - MapWidgetRegInfo widgetInfo = layer.registerSideWidget(control, menuIconId, - widget.getMenuTitle(), "aidl_widget_" + widget.getId(), false, widget.getOrder()); - if (!mapActivity.getMapLayers().getMapWidgetRegistry().isVisible(widgetInfo.key)) { - mapActivity.getMapLayers().getMapWidgetRegistry().setVisibility(widgetInfo, true, false); - } - } - } - } - - private TextInfoWidget createWidgetControl(final MapActivity mapActivity, final String widgetId) { - TextInfoWidget control = new TextInfoWidget(mapActivity) { - @Override - public boolean updateInfo(DrawSettings drawSettings) { - AidlMapWidgetWrapper widget = widgets.get(widgetId); - if (widget != null) { - String txt = widget.getText(); - String subtxt = widget.getDescription(); - boolean night = drawSettings != null && drawSettings.isNightMode(); - int icon = AndroidUtils.getDrawableId(mapActivity.getMyApplication(), night ? widget.getDarkIconName() : widget.getLightIconName()); - setText(txt, subtxt); - if (icon != 0) { - setImageDrawable(icon); - } else { - setImageDrawable(null); - } - return true; - } - return false; - } - }; - control.updateInfo(null); - - control.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - AidlMapWidgetWrapper widget = widgets.get(widgetId); - if (widget != null && widget.getIntentOnClick() != null) { - app.startActivity(widget.getIntentOnClick()); - } - } - }); - return control; - } - - boolean addMapWidget(AidlMapWidgetWrapper widget) { - if (widget != null) { - if (widgets.containsKey(widget.getId())) { - updateMapWidget(widget); - } else { - widgets.put(widget.getId(), widget); - Intent intent = new Intent(); - intent.setAction(AIDL_ADD_MAP_WIDGET); - intent.putExtra(AIDL_OBJECT_ID, widget.getId()); - app.sendBroadcast(intent); - } - app.getAidlApi().refreshMap(); - return true; - } - return false; - } - - boolean removeMapWidget(String widgetId) { - if (!Algorithms.isEmpty(widgetId) && widgets.containsKey(widgetId)) { - widgets.remove(widgetId); - Intent intent = new Intent(); - intent.setAction(AIDL_REMOVE_MAP_WIDGET); - intent.putExtra(AIDL_OBJECT_ID, widgetId); - app.sendBroadcast(intent); - return true; - } - return false; - } - - boolean updateMapWidget(AidlMapWidgetWrapper widget) { - if (widget != null && widgets.containsKey(widget.getId())) { - widgets.put(widget.getId(), widget); - app.getAidlApi().refreshMap(); - return true; - } - return false; - } - - boolean addMapLayer(AidlMapLayerWrapper layer) { - if (layer != null) { - if (layers.containsKey(layer.getId())) { - updateMapLayer(layer); - } else { - layers.put(layer.getId(), layer); - Intent intent = new Intent(); - intent.setAction(AIDL_ADD_MAP_LAYER); - intent.putExtra(AIDL_OBJECT_ID, layer.getId()); - intent.putExtra(AIDL_PACKAGE_NAME, pack); - app.sendBroadcast(intent); - } - app.getAidlApi().refreshMap(); - return true; - } - return false; - } - - boolean removeMapLayer(String layerId) { - if (!Algorithms.isEmpty(layerId) && layers.containsKey(layerId)) { - layers.remove(layerId); - Intent intent = new Intent(); - intent.setAction(AIDL_REMOVE_MAP_LAYER); - intent.putExtra(AIDL_OBJECT_ID, layerId); - app.sendBroadcast(intent); - return true; - } - return false; - } - - boolean updateMapLayer(AidlMapLayerWrapper layer) { - if (layer != null) { - AidlMapLayerWrapper existingLayer = layers.get(layer.getId()); - if (existingLayer != null) { - for (AidlMapPointWrapper point : layer.getPoints()) { - existingLayer.putPoint(point); - } - existingLayer.copyZoomBounds(layer); - app.getAidlApi().refreshMap(); - return true; - } - } - return false; - } - - boolean putMapPoint(String layerId, AidlMapPointWrapper point) { - AidlMapLayerWrapper layer = layers.get(layerId); - if (layer != null) { - layer.putPoint(point); - app.getAidlApi().refreshMap(); - return true; - } - return false; - } - - boolean updateMapPoint(String layerId, AidlMapPointWrapper point, boolean updateOpenedMenuAndMap) { - AidlMapLayerWrapper layer = layers.get(layerId); - if (layer != null) { - layer.putPoint(point); - app.getAidlApi().refreshMap(); - if (updateOpenedMenuAndMap && app.getAidlApi().aMapPointUpdateListener != null) { - app.getAidlApi().aMapPointUpdateListener.onAMapPointUpdated(point, layerId); - } - return true; - } - return false; - } - - boolean removeMapPoint(String layerId, String pointId) { - AidlMapLayerWrapper layer = layers.get(layerId); - if (layer != null) { - layer.removePoint(pointId); - app.getAidlApi().refreshMap(); - return true; - } - return false; - } - - @Override - public int compareTo(@NonNull ConnectedApp app) { - if (name != null && app.name != null) { - return name.compareTo(app.name); - } - return 0; - } - } - public interface SearchCompleteCallback { void onSearchComplete(List resultSet); } diff --git a/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java b/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java index 9e438aefb7..1a4e4e2058 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndAppCustomization.java @@ -16,7 +16,7 @@ import android.widget.ArrayAdapter; import net.osmand.AndroidUtils; import net.osmand.IProgress; import net.osmand.IndexConstants; -import net.osmand.aidl.OsmandAidlApi; +import net.osmand.aidl.ConnectedApp; import net.osmand.data.LocationPoint; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.PluginsActivity; @@ -434,7 +434,7 @@ 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(); - OsmandAidlApi.ConnectedApp connectedApp = app.getAidlApi().getConnectedApp(appPackage); + ConnectedApp connectedApp = app.getAidlApi().getConnectedApp(appPackage); if (connectedApp != null && connectedApp.isEnabled()) { JSONArray jArray = allItems.getJSONArray(appPackage); List list = new ArrayList<>(); diff --git a/OsmAnd/src/net/osmand/plus/activities/PluginsActivity.java b/OsmAnd/src/net/osmand/plus/activities/PluginsActivity.java index 2a6f417909..57d8b79915 100644 --- a/OsmAnd/src/net/osmand/plus/activities/PluginsActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/PluginsActivity.java @@ -16,7 +16,7 @@ import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; -import net.osmand.aidl.OsmandAidlApi.ConnectedApp; +import net.osmand.aidl.ConnectedApp; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; diff --git a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java index a1e062057f..d4025d5b02 100644 --- a/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java +++ b/OsmAnd/src/net/osmand/plus/dialogs/ConfigureMapMenu.java @@ -541,7 +541,7 @@ public class ConfigureMapMenu { srtmPlugin.registerLayerContextMenuActions(activity.getMapView(), adapter, activity); } } - app.getAidlApi().registerLayerContextMenu(activity.getMapView(), adapter, activity); + app.getAidlApi().registerLayerContextMenu(adapter, activity); } public static void refreshMapComplete(final MapActivity activity) { diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java index 6000ea9021..c9d158dea4 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java @@ -29,7 +29,7 @@ import net.osmand.AndroidUtils; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.aidl.OsmandAidlApi; -import net.osmand.aidl.OsmandAidlApi.ConnectedApp; +import net.osmand.aidl.ConnectedApp; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; diff --git a/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java b/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java index 218764da93..a324c1dbef 100644 --- a/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/AidlMapLayer.java @@ -41,7 +41,7 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import static net.osmand.aidl.OsmandAidlApi.ConnectedApp.LAYERS_PREFIX; +import static net.osmand.aidl.ConnectedApp.AIDL_LAYERS_PREFIX; public class AidlMapLayer extends OsmandMapLayer implements IContextMenuProvider, MapTextLayer.MapTextProvider { @@ -92,7 +92,7 @@ public class AidlMapLayer extends OsmandMapLayer implements IContextMenuProvider OsmandApplication app = map.getMyApplication(); layerPref = app.getSettings().registerBooleanPreference(packName + "_" + aidlLayer.getId(), true); - appLayersPref = app.getSettings().registerBooleanPreference(LAYERS_PREFIX + packName, true); + appLayersPref = app.getSettings().registerBooleanPreference(AIDL_LAYERS_PREFIX + packName, true); } @Override From c5311badcd328f9b5592eda58a58731318860770 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 4 Dec 2019 18:07:26 +0200 Subject: [PATCH 6/6] Add profile dependent aidl widgets --- OsmAnd/src/net/osmand/aidl/ConnectedApp.java | 3 +++ OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 1 + 2 files changed, 4 insertions(+) diff --git a/OsmAnd/src/net/osmand/aidl/ConnectedApp.java b/OsmAnd/src/net/osmand/aidl/ConnectedApp.java index a44910100a..112c533175 100644 --- a/OsmAnd/src/net/osmand/aidl/ConnectedApp.java +++ b/OsmAnd/src/net/osmand/aidl/ConnectedApp.java @@ -225,6 +225,7 @@ public class ConnectedApp implements Comparable { Intent intent = new Intent(); intent.setAction(AIDL_ADD_MAP_WIDGET); intent.putExtra(AIDL_OBJECT_ID, widget.getId()); + intent.putExtra(AIDL_PACKAGE_NAME, pack); app.sendBroadcast(intent); } app.getAidlApi().reloadMap(); @@ -239,6 +240,7 @@ public class ConnectedApp implements Comparable { Intent intent = new Intent(); intent.setAction(AIDL_REMOVE_MAP_WIDGET); intent.putExtra(AIDL_OBJECT_ID, widgetId); + intent.putExtra(AIDL_PACKAGE_NAME, pack); app.sendBroadcast(intent); return true; } @@ -278,6 +280,7 @@ public class ConnectedApp implements Comparable { Intent intent = new Intent(); intent.setAction(AIDL_REMOVE_MAP_LAYER); intent.putExtra(AIDL_OBJECT_ID, layerId); + intent.putExtra(AIDL_PACKAGE_NAME, pack); app.sendBroadcast(intent); return true; } diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 637b2ed42c..3e5d21aceb 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -325,6 +325,7 @@ public class OsmandAidlApi { AidlMapWidgetWrapper widget = connectedApp.getWidgets().get(widgetId); MapInfoLayer layer = mapActivity.getMapLayers().getMapInfoLayer(); if (widget != null && layer != null) { + ApplicationMode.regWidgetVisibility(widget.getId(), (ApplicationMode[]) null); TextInfoWidget control = connectedApp.createWidgetControl(mapActivity, widgetId); connectedApp.getWidgetControls().put(widgetId, control); int menuIconId = AndroidUtils.getDrawableId(app, widget.getMenuIconName());