Skip to content

Commit

Permalink
Don't force recreating the map object and associated layer state
Browse files Browse the repository at this point in the history
If Main has been destroyed, both the Map and Logic objects may still be
available and current, so we now reuse them instead of forcing
recreation.

This resolves race conditions that could be caused by selecting and
loading files into layers via the system file picker that could lead to
Main being removed.
  • Loading branch information
simonpoole committed Nov 28, 2024
1 parent 6e7c8ea commit e4e7216
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
27 changes: 16 additions & 11 deletions src/main/java/de/blau/android/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewStub;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
Expand Down Expand Up @@ -492,13 +493,17 @@ protected void onCreate(final Bundle savedInstanceState) {
LinearLayout ml = (LinearLayout) getLayoutInflater().inflate(layout, null);
mapLayout = (RelativeLayout) ml.findViewById(R.id.mainMap);

if (map != null) {
Log.d(DEBUG_TAG, "map exists .. destroying");
map.onDestroy();
Logic logic = App.getLogic(); // logic instance might still be around
map = logic != null ? logic.getMap() : null;
if (map == null) {
map = new Map(this);
map.setId(R.id.map_view);
} else {
ViewGroup parent = (ViewGroup) map.getParent();
if (parent != null) {
parent.removeView(map);
}
}
map = new Map(this);
map.setId(R.id.map_view);

// Register some Listener
mapTouchListener = new MapTouchListener();
map.setOnTouchListener(mapTouchListener);
Expand Down Expand Up @@ -585,14 +590,14 @@ protected void onCreate(final Bundle savedInstanceState) {

loadOnResume = false;

if (App.getLogic() == null) {
if (logic == null) {
Log.i(DEBUG_TAG, "onCreate - creating new logic");
App.newLogic();
logic = App.newLogic();
}
Log.i(DEBUG_TAG, "onCreate - setting new map");

App.getLogic().setPrefs(prefs);
App.getLogic().setMap(map, true);
logic.setPrefs(prefs);
logic.setMap(map, true);

Log.d(DEBUG_TAG, "StorageDelegator dirty is " + App.getDelegator().isDirty());
if (StorageDelegator.isStateAvailable(this) && !App.getDelegator().isDirty()) {
Expand Down Expand Up @@ -806,7 +811,7 @@ public void onError(@Nullable AsyncResult result) {
// loadStateFromFile does this above
App.getLogic().loadEditingState(this, setViewBox);
}
logic.loadLayerState(this, postLoadTasks);
// layer state should still be available if logic is still around, no need to load
map.invalidate();
checkPermissions(() -> {
postLoadData.onSuccess();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/blau/android/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public MapViewLayer getLayer(int index) {
if (index >= 0 && index < mLayers.size()) {
return mLayers.get(index);
}
Log.e(DEBUG_TAG, "Layer for index " + index + " is null");
Log.e(DEBUG_TAG, "Layer for index " + index + " is null " + mLayers.size() + " layers total");
return null;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/de/blau/android/dialogs/Layers.java
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ protected Void doInBackground(Void param) {
* @param map current Map
* @param type the layer type
*/
private void addStyleableLayerFromFile(final FragmentActivity activity, final Preferences prefs, final Map map, @NonNull final LayerType type) {
private void addStyleableLayerFromFile(@NonNull final FragmentActivity activity, @NonNull final Preferences prefs, @NonNull final Map map,
@NonNull final LayerType type) {
Log.d(DEBUG_TAG, "addStyleableLayerFromFile");
SelectFile.read(activity, R.string.config_osmPreferredDir_key, new ReadFile() {
private static final long serialVersionUID = 1L;
Expand Down

0 comments on commit e4e7216

Please sign in to comment.