From 53a3583b3c8d91d54b0482f46b99c3e97bed0249 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Mon, 12 Oct 2020 18:55:21 +0300 Subject: [PATCH] Fix API - Margins should not saved after restart #214 --- .../customization/MapMarginsParams.java | 43 +++++++++------ OsmAnd/src/net/osmand/aidl/ConnectedApp.java | 48 ----------------- OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 53 ++----------------- .../net/osmand/aidl/OsmandAidlServiceV2.java | 5 +- .../osmand/plus/activities/MapActivity.java | 2 +- .../backend/OsmAndAppCustomization.java | 31 +++++++++++ 6 files changed, 66 insertions(+), 116 deletions(-) diff --git a/OsmAnd-api/src/net/osmand/aidlapi/customization/MapMarginsParams.java b/OsmAnd-api/src/net/osmand/aidlapi/customization/MapMarginsParams.java index 8b810a81b4..11ec2ce17f 100644 --- a/OsmAnd-api/src/net/osmand/aidlapi/customization/MapMarginsParams.java +++ b/OsmAnd-api/src/net/osmand/aidlapi/customization/MapMarginsParams.java @@ -3,18 +3,31 @@ package net.osmand.aidlapi.customization; import android.os.Bundle; import android.os.Parcel; +import androidx.annotation.Nullable; + import net.osmand.aidlapi.AidlParams; +import java.util.ArrayList; +import java.util.List; + public class MapMarginsParams extends AidlParams { - private String appModeKey; + public static final String LEFT_MARGIN_KEY = "leftMargin"; + public static final String TOP_MARGIN_KEY = "topMargin"; + public static final String RIGHT_MARGIN_KEY = "rightMargin"; + public static final String BOTTOM_MARGIN_KEY = "bottomMargin"; + public static final String APP_MODES_KEYS_KEY = "appModesKeys"; + private ArrayList appModesKeys = new ArrayList<>(); private int leftMargin; private int topMargin; private int rightMargin; private int bottomMargin; - public MapMarginsParams(String appModeKey, int leftMargin, int topMargin, int rightMargin, int bottomMargin) { - this.appModeKey = appModeKey; + public MapMarginsParams(int leftMargin, int topMargin, int rightMargin, int bottomMargin, + @Nullable List appModesKeys) { + if (appModesKeys != null) { + this.appModesKeys.addAll(appModesKeys); + } this.leftMargin = leftMargin; this.topMargin = topMargin; this.rightMargin = rightMargin; @@ -37,8 +50,8 @@ public class MapMarginsParams extends AidlParams { } }; - public String getAppModeKey() { - return appModeKey; + public List getAppModesKeys() { + return appModesKeys; } public int getLeftMargin() { @@ -59,19 +72,19 @@ public class MapMarginsParams extends AidlParams { @Override public void writeToBundle(Bundle bundle) { - bundle.putString("appModeKey", appModeKey); - bundle.putInt("leftMargin", leftMargin); - bundle.putInt("topMargin", topMargin); - bundle.putInt("rightMargin", rightMargin); - bundle.putInt("bottomMargin", bottomMargin); + bundle.putInt(LEFT_MARGIN_KEY, leftMargin); + bundle.putInt(TOP_MARGIN_KEY, topMargin); + bundle.putInt(RIGHT_MARGIN_KEY, rightMargin); + bundle.putInt(BOTTOM_MARGIN_KEY, bottomMargin); + bundle.putStringArrayList(APP_MODES_KEYS_KEY, appModesKeys); } @Override protected void readFromBundle(Bundle bundle) { - appModeKey = bundle.getString("appModeKey"); - leftMargin = bundle.getInt("leftMargin"); - topMargin = bundle.getInt("topMargin"); - rightMargin = bundle.getInt("rightMargin"); - bottomMargin = bundle.getInt("bottomMargin"); + leftMargin = bundle.getInt(LEFT_MARGIN_KEY); + topMargin = bundle.getInt(TOP_MARGIN_KEY); + rightMargin = bundle.getInt(RIGHT_MARGIN_KEY); + bottomMargin = bundle.getInt(BOTTOM_MARGIN_KEY); + appModesKeys = bundle.getStringArrayList(APP_MODES_KEYS_KEY); } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/aidl/ConnectedApp.java b/OsmAnd/src/net/osmand/aidl/ConnectedApp.java index 02f412887e..8d90bab5e2 100644 --- a/OsmAnd/src/net/osmand/aidl/ConnectedApp.java +++ b/OsmAnd/src/net/osmand/aidl/ConnectedApp.java @@ -11,16 +11,12 @@ import android.widget.CompoundButton; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; - import net.osmand.AndroidUtils; import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuItem; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.settings.backend.OsmandSettings.CommonPreference; import net.osmand.plus.views.OsmandMapLayer; import net.osmand.plus.views.layers.AidlMapLayer; @@ -28,8 +24,6 @@ import net.osmand.plus.views.layers.MapInfoLayer; import net.osmand.plus.views.mapwidgets.widgets.TextInfoWidget; import net.osmand.util.Algorithms; -import java.lang.reflect.Type; -import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -39,7 +33,6 @@ public class ConnectedApp implements Comparable { public static final String AIDL_LAYERS_PREFIX = "aidl_layers_"; public static final String AIDL_WIDGETS_PREFIX = "aidl_widgets_"; - public static final String AIDL_MARGINS_PREFIX = "aidl_margins_"; static final String AIDL_OBJECT_ID = "aidl_object_id"; static final String AIDL_PACKAGE_NAME = "aidl_package_name"; @@ -62,7 +55,6 @@ public class ConnectedApp implements Comparable { private Map mapLayers = new ConcurrentHashMap<>(); private CommonPreference layersPref; - private CommonPreference marginsPref; private String pack; private String name; @@ -76,7 +68,6 @@ public class ConnectedApp implements Comparable { this.pack = pack; this.enabled = enabled; layersPref = app.getSettings().registerBooleanPreference(AIDL_LAYERS_PREFIX + pack, true).cache(); - marginsPref = app.getSettings().registerStringPreference(AIDL_MARGINS_PREFIX + pack, null).cache(); } public boolean isEnabled() { @@ -134,45 +125,6 @@ public class ConnectedApp implements Comparable { } } - void updateMapMargins(@NonNull MapActivity mapActivity) { - String marginsJson = marginsPref.get(); - if (marginsJson != null) { - Type type = new TypeToken>() { - }.getType(); - Map margins = new Gson().fromJson(marginsJson, type); - if (margins != null) { - Integer leftMargin = margins.get("left"); - Integer topMargin = margins.get("top"); - Integer rightMargin = margins.get("right"); - Integer bottomMargin = margins.get("bottom"); - - int left = leftMargin != null ? leftMargin : 0; - int top = topMargin != null ? topMargin : 0; - int right = rightMargin != null ? rightMargin : 0; - int bottom = bottomMargin != null ? bottomMargin : 0; - - mapActivity.setMargins(left, top, right, bottom); - return; - } - } - mapActivity.setMargins(0, 0, 0, 0); - } - - public void setMargins(@NonNull MapActivity mapActivity, String appModeKey, int leftMargin, int topMargin, int rightMargin, int bottomMargin) { - ApplicationMode mode = ApplicationMode.valueOfStringKey(appModeKey, null); - if (mode != null) { - Map margins = new HashMap<>(); - margins.put("left", leftMargin); - margins.put("top", topMargin); - margins.put("right", rightMargin); - margins.put("bottom", bottomMargin); - - String marginsJson = new Gson().toJson(margins); - marginsPref.setModeValue(mode, marginsJson); - updateMapMargins(mapActivity); - } - } - void registerLayerContextMenu(final ContextMenuAdapter menuAdapter, final MapActivity mapActivity) { ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() { diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 6329d5db2e..5801fc916d 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -200,13 +200,6 @@ public class OsmandAidlApi { private static final String AIDL_QUICK_ACTION_NUMBER = "aidl_quick_action_number"; private static final String AIDL_LOCK_STATE = "lock_state"; - private static final String AIDL_SET_MAP_MARGINS = "set_map_margins"; - private static final String AIDL_APP_MODE = "app_mode"; - private static final String AIDL_LEFT_MARGIN = "left_margin"; - private static final String AIDL_TOP_MARGIN = "top_margin"; - private static final String AIDL_RIGHT_MARGIN = "right_margin"; - private static final String AIDL_BOTTOM_MARGIN = "bottom_margin"; - private static final ApplicationMode DEFAULT_PROFILE = ApplicationMode.CAR; private static final ApplicationMode[] VALID_PROFILES = new ApplicationMode[]{ @@ -257,7 +250,6 @@ public class OsmandAidlApi { registerHideSqliteDbFileReceiver(mapActivity); registerExecuteQuickActionReceiver(mapActivity); registerLockStateReceiver(mapActivity); - registerMapMarginsReceiver(mapActivity); initOsmandTelegram(); app.getAppCustomization().addListener(mapActivity); this.mapActivity = mapActivity; @@ -379,28 +371,10 @@ public class OsmandAidlApi { registerReceiver(addMapWidgetReceiver, mapActivity, AIDL_ADD_MAP_WIDGET); } - private void registerMapMarginsReceiver(MapActivity mapActivity) { - final WeakReference mapActivityRef = new WeakReference<>(mapActivity); - BroadcastReceiver addMapWidgetReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - MapActivity mapActivity = mapActivityRef.get(); - String appModeKey = intent.getStringExtra(AIDL_APP_MODE); - String packName = intent.getStringExtra(AIDL_PACKAGE_NAME); - if (mapActivity != null && appModeKey != null && packName != null) { - ConnectedApp connectedApp = connectedApps.get(packName); - if (connectedApp != null) { - int leftMargin = intent.getIntExtra(AIDL_LEFT_MARGIN, 0); - int topMargin = intent.getIntExtra(AIDL_TOP_MARGIN, 0); - int bottomMargin = intent.getIntExtra(AIDL_RIGHT_MARGIN, 0); - int rightMargin = intent.getIntExtra(AIDL_BOTTOM_MARGIN, 0); - - connectedApp.setMargins(mapActivity, appModeKey, leftMargin, topMargin, rightMargin, bottomMargin); - } - } - } - }; - registerReceiver(addMapWidgetReceiver, mapActivity, AIDL_SET_MAP_MARGINS); + boolean setMapMargins(int left, int top, int right, int bottom, @Nullable List appModeKeys) { + app.getAppCustomization().setMapMargins(left, top, right, bottom, appModeKeys); + app.getAppCustomization().updateMapMargins(mapActivity); + return true; } private void registerAddContextMenuButtonsReceiver(MapActivity mapActivity) { @@ -919,12 +893,6 @@ public class OsmandAidlApi { } } - public void updateMapMargins(@NonNull MapActivity mapActivity) { - for (ConnectedApp connectedApp : connectedApps.values()) { - connectedApp.updateMapMargins(mapActivity); - } - } - private void refreshMap() { Intent intent = new Intent(); intent.setAction(AIDL_REFRESH_MAP); @@ -2330,19 +2298,6 @@ public class OsmandAidlApi { return true; } - public boolean setMapMargins(String packName, String appModeKey, int leftMargin, int topMargin, int bottomMargin, int rightMargin) { - Intent intent = new Intent(); - intent.setAction(AIDL_SET_MAP_MARGINS); - intent.putExtra(AIDL_PACKAGE_NAME, packName); - intent.putExtra(AIDL_APP_MODE, appModeKey); - intent.putExtra(AIDL_LEFT_MARGIN, leftMargin); - intent.putExtra(AIDL_TOP_MARGIN, topMargin); - intent.putExtra(AIDL_RIGHT_MARGIN, bottomMargin); - intent.putExtra(AIDL_BOTTOM_MARGIN, rightMargin); - app.sendBroadcast(intent); - return true; - } - public boolean exportProfile(String appModeKey, List settingsTypesKeys) { ApplicationMode appMode = ApplicationMode.valueOfStringKey(appModeKey, null); if (app != null && appMode != null) { diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java index 7c69be1e94..b904ef0517 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlServiceV2.java @@ -1327,9 +1327,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener public boolean setMapMargins(MapMarginsParams params) { try { OsmandAidlApi api = getApi("setMapMargins"); - String packName = getCallingAppPackName(); - return api != null && api.setMapMargins(packName, params.getAppModeKey(), params.getLeftMargin(), - params.getTopMargin(), params.getBottomMargin(), params.getRightMargin()); + return api != null && api.setMapMargins(params.getLeftMargin(), params.getTopMargin(), + params.getBottomMargin(), params.getRightMargin(), params.getAppModesKeys()); } catch (Exception e) { handleException(e); return false; diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 3baa33cb32..e090666915 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -1419,7 +1419,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven }); getMapView().refreshMap(true); applyScreenOrientation(); - app.getAidlApi().updateMapMargins(this); + app.getAppCustomization().updateMapMargins(this); } public void updateNavigationBarColor() { diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmAndAppCustomization.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmAndAppCustomization.java index b23edbbea1..4ee7827668 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmAndAppCustomization.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmAndAppCustomization.java @@ -81,10 +81,16 @@ public class OsmAndAppCustomization { private Set featuresDisabledIds = new HashSet<>(); private Set featuresEnabledPatterns = new HashSet<>(); private Set featuresDisabledPatterns = new HashSet<>(); + private Set marginAppModeUsage = new HashSet<>(); private Map> widgetsVisibilityMap = new LinkedHashMap<>(); private Map> widgetsAvailabilityMap = new LinkedHashMap<>(); private CustomOsmandSettings customOsmandSettings; + private int marginLeft; + private int marginTop; + private int marginRight; + private int marginBottom; + private boolean featuresCustomized; private boolean widgetsCustomized; @@ -151,6 +157,10 @@ public class OsmAndAppCustomization { featuresCustomized = false; widgetsCustomized = false; customOsmandSettings = null; + marginLeft = 0; + marginTop = 0; + marginRight = 0; + marginBottom = 0; restoreOsmandSettings(); featuresEnabledIds.clear(); @@ -159,6 +169,7 @@ public class OsmAndAppCustomization { featuresDisabledPatterns.clear(); widgetsVisibilityMap.clear(); widgetsAvailabilityMap.clear(); + marginAppModeUsage.clear(); return true; } @@ -368,6 +379,26 @@ public class OsmAndAppCustomization { return set; } + public void setMapMargins(int left, int top, int right, int bottom, List appModeKeys) { + marginLeft = left; + marginTop = top; + marginRight = right; + marginBottom = bottom; + marginAppModeUsage.addAll(getAppModesSet(appModeKeys)); + } + + public void updateMapMargins(MapActivity mapActivity) { + if (isMapMarginAvailable()) { + mapActivity.setMargins(marginLeft, marginTop, marginRight, marginBottom); + } else { + mapActivity.setMargins(0, 0, 0, 0); + } + } + + boolean isMapMarginAvailable() { + return marginAppModeUsage.contains(app.getSettings().getApplicationMode()); + } + public boolean isWidgetVisible(@NonNull String key, ApplicationMode appMode) { Set set = widgetsVisibilityMap.get(key); if (set == null) {