Fix API - Margins should not saved after restart #214

This commit is contained in:
Dima-1 2020-10-12 18:55:21 +03:00
parent 57e971a1bb
commit 53a3583b3c
6 changed files with 66 additions and 116 deletions

View file

@ -3,18 +3,31 @@ package net.osmand.aidlapi.customization;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcel; import android.os.Parcel;
import androidx.annotation.Nullable;
import net.osmand.aidlapi.AidlParams; import net.osmand.aidlapi.AidlParams;
import java.util.ArrayList;
import java.util.List;
public class MapMarginsParams extends AidlParams { 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<String> appModesKeys = new ArrayList<>();
private int leftMargin; private int leftMargin;
private int topMargin; private int topMargin;
private int rightMargin; private int rightMargin;
private int bottomMargin; private int bottomMargin;
public MapMarginsParams(String appModeKey, int leftMargin, int topMargin, int rightMargin, int bottomMargin) { public MapMarginsParams(int leftMargin, int topMargin, int rightMargin, int bottomMargin,
this.appModeKey = appModeKey; @Nullable List<String> appModesKeys) {
if (appModesKeys != null) {
this.appModesKeys.addAll(appModesKeys);
}
this.leftMargin = leftMargin; this.leftMargin = leftMargin;
this.topMargin = topMargin; this.topMargin = topMargin;
this.rightMargin = rightMargin; this.rightMargin = rightMargin;
@ -37,8 +50,8 @@ public class MapMarginsParams extends AidlParams {
} }
}; };
public String getAppModeKey() { public List<String> getAppModesKeys() {
return appModeKey; return appModesKeys;
} }
public int getLeftMargin() { public int getLeftMargin() {
@ -59,19 +72,19 @@ public class MapMarginsParams extends AidlParams {
@Override @Override
public void writeToBundle(Bundle bundle) { public void writeToBundle(Bundle bundle) {
bundle.putString("appModeKey", appModeKey); bundle.putInt(LEFT_MARGIN_KEY, leftMargin);
bundle.putInt("leftMargin", leftMargin); bundle.putInt(TOP_MARGIN_KEY, topMargin);
bundle.putInt("topMargin", topMargin); bundle.putInt(RIGHT_MARGIN_KEY, rightMargin);
bundle.putInt("rightMargin", rightMargin); bundle.putInt(BOTTOM_MARGIN_KEY, bottomMargin);
bundle.putInt("bottomMargin", bottomMargin); bundle.putStringArrayList(APP_MODES_KEYS_KEY, appModesKeys);
} }
@Override @Override
protected void readFromBundle(Bundle bundle) { protected void readFromBundle(Bundle bundle) {
appModeKey = bundle.getString("appModeKey"); leftMargin = bundle.getInt(LEFT_MARGIN_KEY);
leftMargin = bundle.getInt("leftMargin"); topMargin = bundle.getInt(TOP_MARGIN_KEY);
topMargin = bundle.getInt("topMargin"); rightMargin = bundle.getInt(RIGHT_MARGIN_KEY);
rightMargin = bundle.getInt("rightMargin"); bottomMargin = bundle.getInt(BOTTOM_MARGIN_KEY);
bottomMargin = bundle.getInt("bottomMargin"); appModesKeys = bundle.getStringArrayList(APP_MODES_KEYS_KEY);
} }
} }

View file

@ -11,16 +11,12 @@ import android.widget.CompoundButton;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuItem; import net.osmand.plus.ContextMenuItem;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; 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.settings.backend.OsmandSettings.CommonPreference;
import net.osmand.plus.views.OsmandMapLayer; import net.osmand.plus.views.OsmandMapLayer;
import net.osmand.plus.views.layers.AidlMapLayer; 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.plus.views.mapwidgets.widgets.TextInfoWidget;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -39,7 +33,6 @@ public class ConnectedApp implements Comparable<ConnectedApp> {
public static final String AIDL_LAYERS_PREFIX = "aidl_layers_"; public static final String AIDL_LAYERS_PREFIX = "aidl_layers_";
public static final String AIDL_WIDGETS_PREFIX = "aidl_widgets_"; 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_OBJECT_ID = "aidl_object_id";
static final String AIDL_PACKAGE_NAME = "aidl_package_name"; static final String AIDL_PACKAGE_NAME = "aidl_package_name";
@ -62,7 +55,6 @@ public class ConnectedApp implements Comparable<ConnectedApp> {
private Map<String, OsmandMapLayer> mapLayers = new ConcurrentHashMap<>(); private Map<String, OsmandMapLayer> mapLayers = new ConcurrentHashMap<>();
private CommonPreference<Boolean> layersPref; private CommonPreference<Boolean> layersPref;
private CommonPreference<String> marginsPref;
private String pack; private String pack;
private String name; private String name;
@ -76,7 +68,6 @@ public class ConnectedApp implements Comparable<ConnectedApp> {
this.pack = pack; this.pack = pack;
this.enabled = enabled; this.enabled = enabled;
layersPref = app.getSettings().registerBooleanPreference(AIDL_LAYERS_PREFIX + pack, true).cache(); layersPref = app.getSettings().registerBooleanPreference(AIDL_LAYERS_PREFIX + pack, true).cache();
marginsPref = app.getSettings().registerStringPreference(AIDL_MARGINS_PREFIX + pack, null).cache();
} }
public boolean isEnabled() { public boolean isEnabled() {
@ -134,45 +125,6 @@ public class ConnectedApp implements Comparable<ConnectedApp> {
} }
} }
void updateMapMargins(@NonNull MapActivity mapActivity) {
String marginsJson = marginsPref.get();
if (marginsJson != null) {
Type type = new TypeToken<HashMap<String, Integer>>() {
}.getType();
Map<String, Integer> 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<String, Integer> 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) { void registerLayerContextMenu(final ContextMenuAdapter menuAdapter, final MapActivity mapActivity) {
ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() { ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() {

View file

@ -200,13 +200,6 @@ public class OsmandAidlApi {
private static final String AIDL_QUICK_ACTION_NUMBER = "aidl_quick_action_number"; 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_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 DEFAULT_PROFILE = ApplicationMode.CAR;
private static final ApplicationMode[] VALID_PROFILES = new ApplicationMode[]{ private static final ApplicationMode[] VALID_PROFILES = new ApplicationMode[]{
@ -257,7 +250,6 @@ public class OsmandAidlApi {
registerHideSqliteDbFileReceiver(mapActivity); registerHideSqliteDbFileReceiver(mapActivity);
registerExecuteQuickActionReceiver(mapActivity); registerExecuteQuickActionReceiver(mapActivity);
registerLockStateReceiver(mapActivity); registerLockStateReceiver(mapActivity);
registerMapMarginsReceiver(mapActivity);
initOsmandTelegram(); initOsmandTelegram();
app.getAppCustomization().addListener(mapActivity); app.getAppCustomization().addListener(mapActivity);
this.mapActivity = mapActivity; this.mapActivity = mapActivity;
@ -379,28 +371,10 @@ public class OsmandAidlApi {
registerReceiver(addMapWidgetReceiver, mapActivity, AIDL_ADD_MAP_WIDGET); registerReceiver(addMapWidgetReceiver, mapActivity, AIDL_ADD_MAP_WIDGET);
} }
private void registerMapMarginsReceiver(MapActivity mapActivity) { boolean setMapMargins(int left, int top, int right, int bottom, @Nullable List<String> appModeKeys) {
final WeakReference<MapActivity> mapActivityRef = new WeakReference<>(mapActivity); app.getAppCustomization().setMapMargins(left, top, right, bottom, appModeKeys);
BroadcastReceiver addMapWidgetReceiver = new BroadcastReceiver() { app.getAppCustomization().updateMapMargins(mapActivity);
@Override return true;
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);
} }
private void registerAddContextMenuButtonsReceiver(MapActivity mapActivity) { 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() { private void refreshMap() {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setAction(AIDL_REFRESH_MAP); intent.setAction(AIDL_REFRESH_MAP);
@ -2330,19 +2298,6 @@ public class OsmandAidlApi {
return true; 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<String> settingsTypesKeys) { public boolean exportProfile(String appModeKey, List<String> settingsTypesKeys) {
ApplicationMode appMode = ApplicationMode.valueOfStringKey(appModeKey, null); ApplicationMode appMode = ApplicationMode.valueOfStringKey(appModeKey, null);
if (app != null && appMode != null) { if (app != null && appMode != null) {

View file

@ -1327,9 +1327,8 @@ public class OsmandAidlServiceV2 extends Service implements AidlCallbackListener
public boolean setMapMargins(MapMarginsParams params) { public boolean setMapMargins(MapMarginsParams params) {
try { try {
OsmandAidlApi api = getApi("setMapMargins"); OsmandAidlApi api = getApi("setMapMargins");
String packName = getCallingAppPackName(); return api != null && api.setMapMargins(params.getLeftMargin(), params.getTopMargin(),
return api != null && api.setMapMargins(packName, params.getAppModeKey(), params.getLeftMargin(), params.getBottomMargin(), params.getRightMargin(), params.getAppModesKeys());
params.getTopMargin(), params.getBottomMargin(), params.getRightMargin());
} catch (Exception e) { } catch (Exception e) {
handleException(e); handleException(e);
return false; return false;

View file

@ -1419,7 +1419,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
}); });
getMapView().refreshMap(true); getMapView().refreshMap(true);
applyScreenOrientation(); applyScreenOrientation();
app.getAidlApi().updateMapMargins(this); app.getAppCustomization().updateMapMargins(this);
} }
public void updateNavigationBarColor() { public void updateNavigationBarColor() {

View file

@ -81,10 +81,16 @@ public class OsmAndAppCustomization {
private Set<String> featuresDisabledIds = new HashSet<>(); private Set<String> featuresDisabledIds = new HashSet<>();
private Set<String> featuresEnabledPatterns = new HashSet<>(); private Set<String> featuresEnabledPatterns = new HashSet<>();
private Set<String> featuresDisabledPatterns = new HashSet<>(); private Set<String> featuresDisabledPatterns = new HashSet<>();
private Set<ApplicationMode> marginAppModeUsage = new HashSet<>();
private Map<String, Set<ApplicationMode>> widgetsVisibilityMap = new LinkedHashMap<>(); private Map<String, Set<ApplicationMode>> widgetsVisibilityMap = new LinkedHashMap<>();
private Map<String, Set<ApplicationMode>> widgetsAvailabilityMap = new LinkedHashMap<>(); private Map<String, Set<ApplicationMode>> widgetsAvailabilityMap = new LinkedHashMap<>();
private CustomOsmandSettings customOsmandSettings; private CustomOsmandSettings customOsmandSettings;
private int marginLeft;
private int marginTop;
private int marginRight;
private int marginBottom;
private boolean featuresCustomized; private boolean featuresCustomized;
private boolean widgetsCustomized; private boolean widgetsCustomized;
@ -151,6 +157,10 @@ public class OsmAndAppCustomization {
featuresCustomized = false; featuresCustomized = false;
widgetsCustomized = false; widgetsCustomized = false;
customOsmandSettings = null; customOsmandSettings = null;
marginLeft = 0;
marginTop = 0;
marginRight = 0;
marginBottom = 0;
restoreOsmandSettings(); restoreOsmandSettings();
featuresEnabledIds.clear(); featuresEnabledIds.clear();
@ -159,6 +169,7 @@ public class OsmAndAppCustomization {
featuresDisabledPatterns.clear(); featuresDisabledPatterns.clear();
widgetsVisibilityMap.clear(); widgetsVisibilityMap.clear();
widgetsAvailabilityMap.clear(); widgetsAvailabilityMap.clear();
marginAppModeUsage.clear();
return true; return true;
} }
@ -368,6 +379,26 @@ public class OsmAndAppCustomization {
return set; return set;
} }
public void setMapMargins(int left, int top, int right, int bottom, List<String> 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) { public boolean isWidgetVisible(@NonNull String key, ApplicationMode appMode) {
Set<ApplicationMode> set = widgetsVisibilityMap.get(key); Set<ApplicationMode> set = widgetsVisibilityMap.get(key);
if (set == null) { if (set == null) {