|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. |
| 3 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | + * Copyright 2016-Present Datadog, Inc. |
| 5 | + */ |
| 6 | +package com.datadog.android.internal.identity |
| 7 | + |
| 8 | +import android.app.Activity |
| 9 | +import android.content.Context |
| 10 | +import android.content.ContextWrapper |
| 11 | +import android.content.res.Resources |
| 12 | +import android.view.View |
| 13 | +import android.view.ViewGroup |
| 14 | +import com.datadog.android.internal.utils.toHexString |
| 15 | +import java.security.MessageDigest |
| 16 | +import java.security.NoSuchAlgorithmException |
| 17 | +import java.util.Collections |
| 18 | +import java.util.Stack |
| 19 | +import java.util.WeakHashMap |
| 20 | +import java.util.concurrent.atomic.AtomicReference |
| 21 | + |
| 22 | +/** |
| 23 | + * Implementation of [ViewIdentityResolver] that generates globally unique, stable identifiers |
| 24 | + * for Android Views by computing and hashing their canonical path in the view hierarchy. |
| 25 | + * |
| 26 | + * Thread-safe: [setCurrentScreen] is called from a RUM worker thread while [onWindowRefreshed] |
| 27 | + * and [resolveViewIdentity] are called from the main thread. |
| 28 | + * |
| 29 | + * @param appIdentifier The application package name used as the root of canonical paths |
| 30 | + */ |
| 31 | +@Suppress("TooManyFunctions") |
| 32 | +class ViewIdentityResolverImpl( |
| 33 | + private val appIdentifier: String |
| 34 | +) : ViewIdentityResolver { |
| 35 | + |
| 36 | + /** |
| 37 | + * Cache: Resource ID (Int) → Resource name (String). |
| 38 | + * Example: 2131230001 → "com.example.app:id/login_button" |
| 39 | + * |
| 40 | + * LRU cache with fixed size. Never explicitly cleared - resource ID mappings |
| 41 | + * are global and don't change based on screen. Avoids repeated calls to |
| 42 | + * resources.getResourceName() which is expensive. |
| 43 | + */ |
| 44 | + @Suppress("UnsafeThirdPartyFunctionCall") // LinkedHashMap constructor doesn't throw |
| 45 | + private val resourceNameCache: MutableMap<Int, String> = Collections.synchronizedMap( |
| 46 | + object : LinkedHashMap<Int, String>(RESOURCE_NAME_CACHE_SIZE, DEFAULT_LOAD_FACTOR, true) { |
| 47 | + override fun removeEldestEntry(eldest: MutableMap.MutableEntry<Int, String>?): Boolean { |
| 48 | + return size > RESOURCE_NAME_CACHE_SIZE |
| 49 | + } |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + /** |
| 54 | + * Cache: View reference → PathData (canonical path + identity hash). |
| 55 | + * Example: Button@0x7f3a → PathData("com.app/view:Home/login_button", "a1b2c3...") |
| 56 | + * |
| 57 | + * WeakHashMap so entries are removed when Views are garbage collected. |
| 58 | + * Cleared when screen changes (paths include screen namespace, so become invalid). |
| 59 | + */ |
| 60 | + @Suppress("UnsafeThirdPartyFunctionCall") // WeakHashMap() is never null |
| 61 | + private val viewPathDataCache: MutableMap<View, PathData> = |
| 62 | + Collections.synchronizedMap(WeakHashMap()) |
| 63 | + |
| 64 | + /** |
| 65 | + * Cache: Root view reference → Screen namespace string. |
| 66 | + * Example: DecorView@0x1a2b → "view:HomeScreen" |
| 67 | + * |
| 68 | + * Avoids recomputing namespace (which may involve walking context chain). |
| 69 | + * Cleared when screen changes (namespace depends on currentRumViewIdentifier). |
| 70 | + */ |
| 71 | + @Suppress("UnsafeThirdPartyFunctionCall") // WeakHashMap() is never null |
| 72 | + private val rootScreenNamespaceCache: MutableMap<View, String> = |
| 73 | + Collections.synchronizedMap(WeakHashMap()) |
| 74 | + |
| 75 | + /** The current RUM view identifier, set via setCurrentScreen(). */ |
| 76 | + private val currentRumViewIdentifier = AtomicReference<String?>(null) |
| 77 | + |
| 78 | + @Synchronized |
| 79 | + override fun setCurrentScreen(identifier: String?) { |
| 80 | + @Suppress("UnsafeThirdPartyFunctionCall") // type-safe: generics prevent VarHandle type mismatches |
| 81 | + val previous = currentRumViewIdentifier.getAndSet(identifier) |
| 82 | + if (previous != identifier) { |
| 83 | + rootScreenNamespaceCache.clear() |
| 84 | + viewPathDataCache.clear() |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Synchronized |
| 89 | + override fun onWindowRefreshed(root: View) { |
| 90 | + indexTree(root) |
| 91 | + } |
| 92 | + |
| 93 | + @Synchronized |
| 94 | + override fun resolveViewIdentity(view: View): String? { |
| 95 | + return viewPathDataCache[view]?.identityHash |
| 96 | + } |
| 97 | + |
| 98 | + private fun indexTree(root: View) { |
| 99 | + val screenNamespace = getScreenNamespace(root) |
| 100 | + val rootCanonicalPath = buildRootCanonicalPath(root, screenNamespace) |
| 101 | + |
| 102 | + traverseAndIndexViews(root, rootCanonicalPath) |
| 103 | + } |
| 104 | + |
| 105 | + /** Builds the canonical path for the root view (used as prefix for all descendants). */ |
| 106 | + private fun buildRootCanonicalPath(root: View, screenNamespace: String): String { |
| 107 | + val rootPathSegment = getViewPathSegment(root, null) |
| 108 | + // Root view (e.g., DecorView) is not interactable, so we don't cache its identity. |
| 109 | + // We only need its path as the prefix for descendant paths. |
| 110 | + return "$appIdentifier/$screenNamespace/$rootPathSegment" |
| 111 | + } |
| 112 | + |
| 113 | + /** Depth-first traversal of view hierarchy, computing and caching identity for each view. */ |
| 114 | + private fun traverseAndIndexViews(root: View, rootCanonicalPath: String) { |
| 115 | + // Index the root view (all cache insertions happen here for consistency) |
| 116 | + md5Hex(rootCanonicalPath)?.let { hash -> |
| 117 | + viewPathDataCache[root] = PathData(rootCanonicalPath, hash) |
| 118 | + } |
| 119 | + |
| 120 | + val stack = Stack<ViewWithCanonicalPath>() |
| 121 | + stack.push(ViewWithCanonicalPath(root, rootCanonicalPath)) |
| 122 | + |
| 123 | + while (stack.isNotEmpty()) { |
| 124 | + val (parent, parentPath) = stack.pop() |
| 125 | + if (parent is ViewGroup) { |
| 126 | + indexChildrenOf(parent, parentPath, stack) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private fun indexChildrenOf( |
| 132 | + parent: ViewGroup, |
| 133 | + parentPath: String, |
| 134 | + stack: Stack<ViewWithCanonicalPath> |
| 135 | + ) { |
| 136 | + for (i in 0 until parent.childCount) { |
| 137 | + val child = parent.getChildAt(i) |
| 138 | + val childPath = "$parentPath/${getViewPathSegment(child, parent)}" |
| 139 | + val childHash = md5Hex(childPath) ?: continue |
| 140 | + |
| 141 | + viewPathDataCache[child] = PathData(childPath, childHash) |
| 142 | + stack.push(ViewWithCanonicalPath(child, childPath)) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private fun getScreenNamespace(rootView: View): String { |
| 147 | + rootScreenNamespaceCache[rootView]?.let { return it } |
| 148 | + |
| 149 | + val screenNamespace = getNamespaceFromRumView() |
| 150 | + ?: getNamespaceFromActivity(rootView) |
| 151 | + ?: getNamespaceFromRootResourceId(rootView) |
| 152 | + ?: getNamespaceFromRootClassName(rootView) |
| 153 | + |
| 154 | + rootScreenNamespaceCache[rootView] = screenNamespace |
| 155 | + return screenNamespace |
| 156 | + } |
| 157 | + |
| 158 | + /** Priority 1: Use RUM view identifier if available (set via RumMonitor.startView). */ |
| 159 | + private fun getNamespaceFromRumView(): String? { |
| 160 | + return currentRumViewIdentifier.get()?.let { viewName -> |
| 161 | + "$NAMESPACE_VIEW_PREFIX${escapePathComponent(viewName)}" |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** Priority 2: Fall back to Activity class name if root view has Activity context. */ |
| 166 | + private fun getNamespaceFromActivity(rootView: View): String? { |
| 167 | + return findActivity(rootView)?.let { activity -> |
| 168 | + "$NAMESPACE_ACTIVITY_PREFIX${escapePathComponent(activity::class.java.name)}" |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + /** Priority 3: Fall back to root view's resource ID if it has one. */ |
| 173 | + private fun getNamespaceFromRootResourceId(rootView: View): String? { |
| 174 | + return getResourceName(rootView)?.let { resourceName -> |
| 175 | + "$NAMESPACE_ROOT_ID_PREFIX${escapePathComponent(resourceName)}" |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /** Priority 4: Last resort - use root view's class name. */ |
| 180 | + private fun getNamespaceFromRootClassName(rootView: View): String { |
| 181 | + return "$NAMESPACE_ROOT_CLASS_PREFIX${escapePathComponent(rootView.javaClass.name)}" |
| 182 | + } |
| 183 | + |
| 184 | + private fun getViewPathSegment(view: View, parentView: ViewGroup?): String { |
| 185 | + val resourceName = getResourceName(view) |
| 186 | + if (resourceName != null) return escapePathComponent(resourceName) |
| 187 | + |
| 188 | + val siblingIndex = countPrecedingSiblingsOfSameClass(view, parentView) |
| 189 | + return "$LOCAL_KEY_CLASS_PREFIX${escapePathComponent(view.javaClass.name)}#$siblingIndex" |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Counts how many siblings of the same class appear before this view in the parent. |
| 194 | + * Used to disambiguate views without resource IDs (e.g., "TextView#0", "TextView#1"). |
| 195 | + * Returns 0 if parentView is null (root view case). |
| 196 | + */ |
| 197 | + private fun countPrecedingSiblingsOfSameClass(view: View, parentView: ViewGroup?): Int { |
| 198 | + if (parentView == null) return 0 |
| 199 | + |
| 200 | + var count = 0 |
| 201 | + val viewClass = view.javaClass |
| 202 | + for (i in 0 until parentView.childCount) { |
| 203 | + val sibling = parentView.getChildAt(i) |
| 204 | + if (sibling === view) break |
| 205 | + if (sibling.javaClass == viewClass) count++ |
| 206 | + } |
| 207 | + return count |
| 208 | + } |
| 209 | + |
| 210 | + private fun getResourceName(view: View): String? { |
| 211 | + val id = view.id |
| 212 | + if (id == View.NO_ID) return null |
| 213 | + |
| 214 | + return resourceNameCache[id] ?: try { |
| 215 | + view.resources?.getResourceName(id)?.also { name -> |
| 216 | + resourceNameCache[id] = name |
| 217 | + } |
| 218 | + } catch (_: Resources.NotFoundException) { |
| 219 | + null |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + private data class ViewWithCanonicalPath(val view: View, val canonicalPath: String) |
| 224 | + private data class PathData(val canonicalPath: String, val identityHash: String) |
| 225 | + |
| 226 | + companion object { |
| 227 | + private const val RESOURCE_NAME_CACHE_SIZE = 500 |
| 228 | + private const val DEFAULT_LOAD_FACTOR = 0.75f |
| 229 | + private const val NAMESPACE_VIEW_PREFIX = "view:" |
| 230 | + private const val NAMESPACE_ACTIVITY_PREFIX = "act:" |
| 231 | + private const val NAMESPACE_ROOT_ID_PREFIX = "root-id:" |
| 232 | + private const val NAMESPACE_ROOT_CLASS_PREFIX = "root-cls:" |
| 233 | + private const val LOCAL_KEY_CLASS_PREFIX = "cls:" |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +private fun escapePathComponent(input: String): String { |
| 238 | + return input.replace("%", "%25").replace("/", "%2F") |
| 239 | +} |
| 240 | + |
| 241 | +private fun md5Hex(input: String): String? { |
| 242 | + return try { |
| 243 | + val messageDigest = MessageDigest.getInstance("MD5") |
| 244 | + messageDigest.update(input.toByteArray(Charsets.UTF_8)) |
| 245 | + messageDigest.digest().toHexString() |
| 246 | + } catch (@Suppress("TooGenericExceptionCaught", "SwallowedException") _: NoSuchAlgorithmException) { |
| 247 | + null |
| 248 | + } |
| 249 | +} |
| 250 | + |
| 251 | +@Suppress("ReturnCount") |
| 252 | +private fun findActivity(view: View): Activity? { |
| 253 | + var ctx: Context? = view.context ?: return null |
| 254 | + while (ctx is ContextWrapper) { |
| 255 | + if (ctx is Activity) return ctx |
| 256 | + ctx = ctx.baseContext |
| 257 | + } |
| 258 | + return null |
| 259 | +} |
0 commit comments