Skip to content

Commit c696aa0

Browse files
authored
Merge pull request #2664 from DataDog/nogorodnikov/rum-9973/replace-addfirst-removefirst-usages
RUM-9973: Remove `addFirst`, `removeFirst`, `removeLast` usages
2 parents 131a0a2 + 7af55f5 commit c696aa0

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

detekt_custom.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ datadog:
253253
# endregion
254254
# region Java Collections
255255
- "java.util.Collections.newSetFromMap(kotlin.collections.MutableMap?):java.lang.IllegalArgumentException"
256+
- "java.util.LinkedList.add(kotlin.Int, android.view.View):java.lang.IndexOutOfBoundsException"
257+
- "java.util.LinkedList.add(kotlin.Int, com.datadog.android.webview.internal.rum.domain.WebViewNativeRumViewsCache.ViewEntry):java.lang.IndexOutOfBoundsException"
256258
- "java.util.LinkedList.offer(com.datadog.android.core.internal.data.upload.UploadWorker.UploadNextBatchTask):java.lang.ClassCastException,java.lang.NullPointerException,java.lang.IllegalArgumentException"
257259
- "java.util.LinkedList.removeFirst():java.util.NoSuchElementException"
258260
- "java.util.LinkedList.removeLast():java.util.NoSuchElementException"
@@ -652,15 +654,19 @@ datadog:
652654
- "java.util.LinkedList.addAll(kotlin.collections.Collection)"
653655
- "java.util.LinkedList.addAll(kotlin.Int, kotlin.collections.Collection)"
654656
- "java.util.LinkedList.addFirst(android.view.View?)"
657+
- "java.util.LinkedList.addFirst(com.datadog.android.webview.internal.rum.domain.WebViewNativeRumViewsCache.ViewEntry?)"
655658
- "java.util.LinkedList.clear()"
656659
- "java.util.LinkedList.constructor()"
657660
- "java.util.LinkedList.constructor(kotlin.collections.MutableCollection?)"
658661
- "java.util.LinkedList.firstOrNull(kotlin.Function1)"
659662
- "java.util.LinkedList.forEach(kotlin.Function1)"
660663
- "java.util.LinkedList.isEmpty()"
661664
- "java.util.LinkedList.isNotEmpty()"
665+
- "java.util.LinkedList.iterator()"
666+
- "java.util.LinkedList.peekLast()"
662667
- "java.util.LinkedList.poll()"
663668
- "java.util.LinkedList.remove(com.datadog.android.privacy.TrackingConsentProviderCallback)"
669+
- "java.util.LinkedList.remove(com.datadog.android.webview.internal.rum.domain.WebViewNativeRumViewsCache.ViewEntry)"
664670
- "java.util.LinkedHashMap.remove(kotlin.String)"
665671
- "java.util.Queue.addAll(kotlin.collections.Collection)"
666672
- "java.util.Queue.clear()"
@@ -820,10 +826,6 @@ datadog:
820826
- "java.util.HashMap.clear()"
821827
- "java.util.HashSet.addAll(kotlin.collections.Collection)"
822828
- "java.util.HashSet.find(kotlin.Function1)"
823-
- "java.util.LinkedList.addFirst(com.datadog.android.webview.internal.rum.domain.WebViewNativeRumViewsCache.ViewEntry?)"
824-
- "java.util.LinkedList.peekLast()"
825-
- "java.util.LinkedList.remove(com.datadog.android.webview.internal.rum.domain.WebViewNativeRumViewsCache.ViewEntry)"
826-
- "java.util.LinkedList.iterator()"
827829
- "java.util.Properties.constructor()"
828830
- "java.util.Properties.setProperty(kotlin.String?, kotlin.String?)"
829831
- "java.util.UUID.constructor(kotlin.Long, kotlin.Long)"

features/dd-sdk-android-rum/src/main/kotlin/com/datadog/android/rum/internal/instrumentation/gestures/GesturesListener.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,19 @@ internal class GesturesListener(
135135
isScroll: Boolean = false
136136
): ViewTarget? {
137137
val queue = LinkedList<View>()
138-
queue.addFirst(decorView)
138+
// add(index, element) instead of addFirst here is on purpose, to prevent issues with old AGP being used
139+
// when compiling with Android API 35.
140+
// Index 0 is always safe
141+
@Suppress("UnsafeThirdPartyFunctionCall")
142+
queue.add(0, decorView)
139143
var target: ViewTarget? = null
140144
var composeViewDetected = false
141145
while (queue.isNotEmpty()) {
142-
// removeFirst can't fail because we checked isNotEmpty
146+
// removeAt(index) instead of removeFirst here is on purpose, to prevent issues
147+
// with old AGP being used when compiling with Android API 35.
148+
// removeAt can't fail because we checked isNotEmpty
143149
@Suppress("UnsafeThirdPartyFunctionCall")
144-
val view = queue.removeFirst()
150+
val view = queue.removeAt(0)
145151
composeViewDetected = composeViewDetected || isJetpackComposeView(view)
146152
val newTarget = if (isScroll) {
147153
findTargetForScroll(view, x, y)

features/dd-sdk-android-trace/src/main/java/com/datadog/opentracing/PendingTrace.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ public synchronized boolean clean() {
362362
@Override
363363
public void addFirst(final DDSpan span) {
364364
synchronized (this) {
365+
// add(index, element) instead of addFirst here is on purpose, to prevent issues
366+
// with old AGP being used when compiling with Android API 35.
365367
super.add(0, span);
366368
}
367369
completedSpanCount.incrementAndGet();

features/dd-sdk-android-webview/src/main/kotlin/com/datadog/android/webview/internal/rum/domain/WebViewNativeRumViewsCache.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,24 @@ internal class WebViewNativeRumViewsCache(
6363
parentViewsHistoryQueue.first.timestamp <= entry.timestamp
6464
)
6565
) {
66-
parentViewsHistoryQueue.addFirst(entry)
66+
// add(index, element) instead of addFirst here is on purpose, to prevent issues
67+
// with old AGP being used when compiling with Android API 35.
68+
// Index 0 is always safe
69+
@Suppress("UnsafeThirdPartyFunctionCall")
70+
parentViewsHistoryQueue.add(0, entry)
6771
} else if (parentViewsHistoryQueue.first.viewId == entry.viewId) {
6872
// the function is synchronized and we are checking the size before
73+
if (parentViewsHistoryQueue.isNotEmpty()) {
74+
// removeAt(index) instead of removeFirst here is on purpose, to prevent issues
75+
// with old AGP being used when compiling with Android API 35.
76+
@Suppress("UnsafeThirdPartyFunctionCall")
77+
parentViewsHistoryQueue.removeAt(0)
78+
}
79+
// add(index, element) instead of addFirst here is on purpose, to prevent issues
80+
// with old AGP being used when compiling with Android API 35.
81+
// Index 0 is always safe
6982
@Suppress("UnsafeThirdPartyFunctionCall")
70-
parentViewsHistoryQueue.removeFirst()
71-
parentViewsHistoryQueue.addFirst(entry)
83+
parentViewsHistoryQueue.add(0, entry)
7284
}
7385
}
7486

@@ -84,9 +96,11 @@ internal class WebViewNativeRumViewsCache(
8496
}
8597
}
8698
while (parentViewsHistoryQueue.size > DATA_CACHE_ENTRIES_LIMIT) {
99+
// removeAt(index) instead of removeLast here is on purpose, to prevent issues
100+
// with old AGP being used when compiling with Android API 35.
87101
// the function is synchronized and we are checking the size before
88102
@Suppress("UnsafeThirdPartyFunctionCall")
89-
parentViewsHistoryQueue.removeLast()
103+
parentViewsHistoryQueue.removeAt(parentViewsHistoryQueue.size - 1)
90104
}
91105
}
92106

0 commit comments

Comments
 (0)