Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid TTLE possibly caused by saving elements with parent relations #2715

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 55 additions & 6 deletions src/main/java/de/blau/android/dialogs/UploadConflict.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ public class UploadConflict extends ImmersiveDialogFragment {
private static final int TAG_LEN = Math.min(LOG_TAG_LEN, UploadConflict.class.getSimpleName().length());
private static final String DEBUG_TAG = UploadConflict.class.getSimpleName().substring(0, TAG_LEN);

private static final String CONFLICT_KEY = "uploadresult";
private static final String ELEMENTS_KEY = "elements";
private static final String CONFLICT_KEY = "uploadresult";
private static final String ELEMENT_IDS_KEY = "ids";
private static final String ELEMENT_TYPES_KEY = "types";

private static final String TAG = "fragment_upload_conflict";

Expand Down Expand Up @@ -152,7 +153,7 @@ private static UploadConflict newInstance(@NonNull final Conflict conflict, List
Bundle args = new Bundle();
args.putSerializable(CONFLICT_KEY, conflict);
if (elements != null) {
args.putSerializable(ELEMENTS_KEY, new ArrayList<>(elements));
putElementsInBundle(elements, args);
}

f.setArguments(args);
Expand All @@ -167,16 +168,64 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
if (savedInstanceState != null) {
Log.d(DEBUG_TAG, "restoring from saved state");
conflict = de.blau.android.util.Util.getSerializeable(savedInstanceState, CONFLICT_KEY, Conflict.class);
elements = Util.getSerializeableArrayList(savedInstanceState, ELEMENTS_KEY, OsmElement.class);
elements = getElementsFromBundle(savedInstanceState);
} else {
conflict = de.blau.android.util.Util.getSerializeable(getArguments(), CONFLICT_KEY, Conflict.class);
elements = Util.getSerializeableArrayList(getArguments(), ELEMENTS_KEY, OsmElement.class);
elements = getElementsFromBundle(getArguments());
}
}

/**
* Put element ids and types in to a bundle
*
* @param elements a List of OsmELement
* @param bundle the target bundle
*/
private static void putElementsInBundle(@NonNull List<OsmElement> elements, @NonNull Bundle bundle) {
ArrayList<Long> ids = new ArrayList<>();
ArrayList<String> types = new ArrayList<>();
for (OsmElement e : elements) {
ids.add(e.getOsmId());
types.add(e.getName());
}
bundle.putStringArrayList(ELEMENT_TYPES_KEY, types);
bundle.putSerializable(ELEMENT_IDS_KEY, ids);
}

/**
* Get elements from bundle if any
*
* @param bundle the bundle
* @return return a List of elements or null
*/
@Nullable
private List<OsmElement> getElementsFromBundle(@NonNull Bundle bundle) {
List<OsmElement> result = new ArrayList<>();
List<Long> ids = Util.getSerializeableArrayList(bundle, ELEMENT_IDS_KEY, Long.class);
List<String> types = bundle.getStringArrayList(ELEMENT_TYPES_KEY);
if (ids != null && types != null) {
if (ids.size() != types.size()) {
throw new IllegalArgumentException("Mismatched ids types size " + ids.size() + " != " + types.size());
}
StorageDelegator delegator = App.getDelegator();
for (int i = 0; i < ids.size(); i++) {
final String elementType = types.get(i);
final long osmId = ids.get(i);
OsmElement e = delegator.getOsmElement(elementType, osmId);
if (e == null) {
throw new IllegalStateException(elementType + " " + osmId + " not in memory");
}
result.add(e);
}
return result;
}
return null;
}

@NonNull
@Override
public AppCompatDialog onCreateDialog(Bundle savedInstanceState) {
Log.i(DEBUG_TAG, conflict.getClass().getCanonicalName() + (elements != null ? elements.size() + " elements" : ""));
Builder builder = new AlertDialog.Builder(getActivity());
builder.setIcon(ThemeUtils.getResIdFromAttribute(getActivity(), R.attr.alert_dialog));
builder.setTitle(R.string.upload_conflict_title);
Expand Down Expand Up @@ -531,7 +580,7 @@ public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(CONFLICT_KEY, conflict);
if (elements != null) {
outState.putSerializable(ELEMENTS_KEY, new ArrayList<>(elements));
putElementsInBundle(elements, outState);
}
}
}
Loading