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 potential TTLE in ReviewAndUpload modal #2750

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions src/main/java/de/blau/android/dialogs/AbstractReviewDialog.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.blau.android.dialogs;

import static de.blau.android.contract.Constants.LOG_TAG_LEN;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
Expand Down Expand Up @@ -38,10 +40,10 @@
*/
public abstract class AbstractReviewDialog extends ImmersiveDialogFragment {

private static final String DEBUG_TAG = AbstractReviewDialog.class.getSimpleName().substring(0, Math.min(23, AbstractReviewDialog.class.getSimpleName().length()));
private static final int TAG_LEN = Math.min(LOG_TAG_LEN, AbstractReviewDialog.class.getSimpleName().length());
private static final String DEBUG_TAG = AbstractReviewDialog.class.getSimpleName().substring(0, TAG_LEN);

protected static final String TAG_KEY = "tag";
protected static final String ELEMENTS_KEY = "elements";
protected static final String TAG_KEY = "tag";

protected List<OsmElement> elements = null;

Expand Down Expand Up @@ -256,7 +258,7 @@ public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.d(DEBUG_TAG, "onSaveInstanceState");
if (elements != null) {
outState.putSerializable(ELEMENTS_KEY, new ArrayList<>(elements));
Util.putElementsInBundle(elements, outState);
}
}
}
5 changes: 4 additions & 1 deletion src/main/java/de/blau/android/dialogs/Review.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.blau.android.dialogs;

import static de.blau.android.contract.Constants.LOG_TAG_LEN;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
Expand All @@ -20,7 +22,8 @@
*/
public class Review extends AbstractReviewDialog {

private static final String DEBUG_TAG = Review.class.getSimpleName().substring(0, Math.min(23, Review.class.getSimpleName().length()));
private static final int TAG_LEN = Math.min(LOG_TAG_LEN, Review.class.getSimpleName().length());
private static final String DEBUG_TAG = Review.class.getSimpleName().substring(0, TAG_LEN);

public static final String TAG = "fragment_review";

Expand Down
11 changes: 7 additions & 4 deletions src/main/java/de/blau/android/dialogs/ReviewAndUpload.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.blau.android.dialogs;

import static de.blau.android.contract.Constants.LOG_TAG_LEN;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -54,7 +56,8 @@
*/
public class ReviewAndUpload extends AbstractReviewDialog {

private static final String DEBUG_TAG = ReviewAndUpload.class.getSimpleName().substring(0, Math.min(23, ReviewAndUpload.class.getSimpleName().length()));
private static final int TAG_LEN = Math.min(LOG_TAG_LEN, ReviewAndUpload.class.getSimpleName().length());
private static final String DEBUG_TAG = ReviewAndUpload.class.getSimpleName().substring(0, TAG_LEN);

public static final String TAG = "fragment_confirm_upload";

Expand Down Expand Up @@ -106,7 +109,7 @@ private static ReviewAndUpload newInstance(@Nullable List<OsmElement> elements)
Bundle args = new Bundle();
args.putString(TAG_KEY, TAG);
if (elements != null) {
args.putSerializable(ELEMENTS_KEY, new ArrayList<>(elements));
de.blau.android.dialogs.Util.putElementsInBundle(elements, args);
}
f.setArguments(args);
f.setShowsDialog(true);
Expand All @@ -120,9 +123,9 @@ public AppCompatDialog onCreateDialog(Bundle savedInstanceState) {
Log.d(DEBUG_TAG, "onCreateDialog");
if (savedInstanceState != null) {
Log.d(DEBUG_TAG, "restoring from saved state");
elements = Util.getSerializeableArrayList(savedInstanceState, ELEMENTS_KEY, OsmElement.class);
elements = de.blau.android.dialogs.Util.getElementsFromBundle(savedInstanceState);
} else {
elements = Util.getSerializeableArrayList(getArguments(), ELEMENTS_KEY, OsmElement.class);
elements = de.blau.android.dialogs.Util.getElementsFromBundle(getArguments());
}

FragmentActivity activity = getActivity();
Expand Down
61 changes: 5 additions & 56 deletions src/main/java/de/blau/android/dialogs/UploadConflict.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static de.blau.android.contract.Constants.LOG_TAG_LEN;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -58,7 +57,6 @@
import de.blau.android.util.InfoDialogFragment;
import de.blau.android.util.ScreenMessage;
import de.blau.android.util.ThemeUtils;
import de.blau.android.util.Util;

/**
* Dialog to resolve upload conflicts one by one
Expand All @@ -70,9 +68,7 @@ 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 ELEMENT_IDS_KEY = "ids";
private static final String ELEMENT_TYPES_KEY = "types";
private static final String CONFLICT_KEY = "uploadresult";

private static final String TAG = "fragment_upload_conflict";

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

f.setArguments(args);
Expand All @@ -168,60 +164,13 @@ 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 = getElementsFromBundle(savedInstanceState);
elements = de.blau.android.dialogs.Util.getElementsFromBundle(savedInstanceState);
} else {
conflict = de.blau.android.util.Util.getSerializeable(getArguments(), CONFLICT_KEY, Conflict.class);
elements = getElementsFromBundle(getArguments());
elements = de.blau.android.dialogs.Util.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) {
Expand Down Expand Up @@ -580,7 +529,7 @@ public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(CONFLICT_KEY, conflict);
if (elements != null) {
putElementsInBundle(elements, outState);
Util.putElementsInBundle(elements, outState);
}
}
}
66 changes: 64 additions & 2 deletions src/main/java/de/blau/android/dialogs/Util.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package de.blau.android.dialogs;

import static de.blau.android.contract.Constants.LOG_TAG_LEN;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import de.blau.android.App;
import de.blau.android.osm.OsmElement;
import de.blau.android.osm.StorageDelegator;

public final class Util {
private static final String DEBUG_TAG = Util.class.getSimpleName().substring(0, Math.min(23, Util.class.getSimpleName().length()));

private static final int TAG_LEN = Math.min(LOG_TAG_LEN, Util.class.getSimpleName().length());
private static final String DEBUG_TAG = Util.class.getSimpleName().substring(0, TAG_LEN);

private static final String ELEMENT_IDS_KEY = "ids";
private static final String ELEMENT_TYPES_KEY = "types";

/**
* Disallow instantiation
Expand Down Expand Up @@ -43,7 +58,7 @@ public static void dismissDialog(@NonNull Fragment fragment, @NonNull String tag
* @param fm the FragmentManager
* @param tag the tag
*/
private static void dismiss(FragmentManager fm, @NonNull String tag) {
static void dismiss(FragmentManager fm, @NonNull String tag) {
FragmentTransaction ft = fm.beginTransaction();
Fragment fragment = fm.findFragmentByTag(tag);
if (fragment != null) {
Expand All @@ -55,4 +70,51 @@ private static void dismiss(FragmentManager fm, @NonNull String tag) {
Log.e(DEBUG_TAG, "dismissDialog " + tag, isex);
}
}

/**
* Put element ids and types in to a bundle
*
* @param elements a List of OsmELement
* @param bundle the target bundle
*/
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
static List<OsmElement> getElementsFromBundle(@NonNull Bundle bundle) {
List<OsmElement> result = new ArrayList<>();
List<Long> ids = de.blau.android.util.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;
}
}
Loading