From 621c25b4375b8080095c8b19e725e4a5c5ff7708 Mon Sep 17 00:00:00 2001 From: simonpoole Date: Sat, 14 Dec 2024 14:26:03 +0100 Subject: [PATCH] Avoid potential TTLE in ReviewAndUpload modal --- .../android/dialogs/AbstractReviewDialog.java | 10 +-- .../java/de/blau/android/dialogs/Review.java | 5 +- .../blau/android/dialogs/ReviewAndUpload.java | 11 ++-- .../blau/android/dialogs/UploadConflict.java | 61 ++--------------- .../java/de/blau/android/dialogs/Util.java | 66 ++++++++++++++++++- 5 files changed, 86 insertions(+), 67 deletions(-) diff --git a/src/main/java/de/blau/android/dialogs/AbstractReviewDialog.java b/src/main/java/de/blau/android/dialogs/AbstractReviewDialog.java index a3a9a28c55..1d7a2dcbb0 100644 --- a/src/main/java/de/blau/android/dialogs/AbstractReviewDialog.java +++ b/src/main/java/de/blau/android/dialogs/AbstractReviewDialog.java @@ -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; @@ -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 elements = null; @@ -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); } } } diff --git a/src/main/java/de/blau/android/dialogs/Review.java b/src/main/java/de/blau/android/dialogs/Review.java index 6d0d3f056f..0adf634fc6 100644 --- a/src/main/java/de/blau/android/dialogs/Review.java +++ b/src/main/java/de/blau/android/dialogs/Review.java @@ -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; @@ -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"; diff --git a/src/main/java/de/blau/android/dialogs/ReviewAndUpload.java b/src/main/java/de/blau/android/dialogs/ReviewAndUpload.java index 88c009718f..fd81480e9e 100644 --- a/src/main/java/de/blau/android/dialogs/ReviewAndUpload.java +++ b/src/main/java/de/blau/android/dialogs/ReviewAndUpload.java @@ -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; @@ -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"; @@ -106,7 +109,7 @@ private static ReviewAndUpload newInstance(@Nullable List 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); @@ -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(); diff --git a/src/main/java/de/blau/android/dialogs/UploadConflict.java b/src/main/java/de/blau/android/dialogs/UploadConflict.java index e551a1cdbc..2c09692f90 100644 --- a/src/main/java/de/blau/android/dialogs/UploadConflict.java +++ b/src/main/java/de/blau/android/dialogs/UploadConflict.java @@ -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; @@ -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 @@ -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"; @@ -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); @@ -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 elements, @NonNull Bundle bundle) { - ArrayList ids = new ArrayList<>(); - ArrayList 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 getElementsFromBundle(@NonNull Bundle bundle) { - List result = new ArrayList<>(); - List ids = Util.getSerializeableArrayList(bundle, ELEMENT_IDS_KEY, Long.class); - List 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) { @@ -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); } } } diff --git a/src/main/java/de/blau/android/dialogs/Util.java b/src/main/java/de/blau/android/dialogs/Util.java index 1138872feb..d183c69fcc 100644 --- a/src/main/java/de/blau/android/dialogs/Util.java +++ b/src/main/java/de/blau/android/dialogs/Util.java @@ -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 @@ -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) { @@ -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 elements, @NonNull Bundle bundle) { + ArrayList ids = new ArrayList<>(); + ArrayList 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 getElementsFromBundle(@NonNull Bundle bundle) { + List result = new ArrayList<>(); + List ids = de.blau.android.util.Util.getSerializeableArrayList(bundle, ELEMENT_IDS_KEY, Long.class); + List 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; + } }