Skip to content

Refactor getUserAgent method and improve logging#6

Open
yzheka wants to merge 1 commit intoOpenpanel-dev:mainfrom
yzheka:patch-1
Open

Refactor getUserAgent method and improve logging#6
yzheka wants to merge 1 commit intoOpenpanel-dev:mainfrom
yzheka:patch-1

Conversation

@yzheka
Copy link

@yzheka yzheka commented Feb 10, 2026

On some devices WebView(context).settings.userAgentString can throw NullPointerException, preventing for fallback to System.getProperty("http.agent"). With this, if webview fails, system property will be fetched. If system property fetch fails too then no user-agent header will be added, letting system to decide

Summary by CodeRabbit

  • New Features

    • Added verbose logging option to configuration for enhanced debugging capabilities.
    • Introduced new environment detection API method.
  • Improvements

    • Refined header initialization flow for improved reliability.
    • Enhanced system information handling for better consistency.

On some devices WebView(context).settings.userAgentString can throw NullPointerException, preventing for fallback to System.getProperty("http.agent"). With this, if webview fails, system property will be fetched. If system property fetch fails too then no user-agent header will be added, letting system to decide
@coderabbitai
Copy link

coderabbitai bot commented Feb 10, 2026

📝 Walkthrough

Walkthrough

Modified the OpenPanel SDK to add verbose logging configuration via new Boolean fields, introduce a platform detection method, and refactor user-agent header initialization from eager to conditional based on nullability. Minor logging formatting adjustments applied across the codebase.

Changes

Cohort / File(s) Summary
OpenPanel SDK Configuration & Header Refactoring
OpenPanelSDK/src/main/java/com/dev/openpanelsdk/OpenPanel.kt
Added verbose: Boolean field to Options and Api.Config data classes; changed getUserAgent() return type to nullable String? with conditional header assignment; introduced new public isAndroidEnvironment(): Boolean method; refactored header initialization flow to defer user-agent addition; minor log formatting consistency improvements.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A verbose rabbit's delight,
Headers flowing just right,
Null-aware and keen,
New methods so clean,
SDK springs forth with fresh light!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main changes: refactoring getUserAgent to handle null cases and adding verbose logging capabilities.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (3)
OpenPanelSDK/src/main/java/com/dev/openpanelsdk/OpenPanel.kt (3)

262-262: ⚠️ Potential issue | 🟠 Major

Public isAndroidEnvironment() always returns false — this is an Android SDK.

This method is exposed as a public API but hard-codes false. Meanwhile, the companion object (Line 46) has a private implementation that actually performs the check but is never called. This will mislead any consumer relying on this method.

Either delegate to the companion's implementation, or remove both methods if they serve no purpose in an Android-only SDK.


36-36: ⚠️ Potential issue | 🟡 Minor

Nit: remove the inline comment // Add this line.

This appears to be a review instruction left in the code. Same applies to Line 79 and Line 369.


406-408: ⚠️ Potential issue | 🟠 Major

Verbose logging leaks openpanel-client-secret header value.

When verbose is enabled, all headers (including openpanel-client-secret) are logged via logVerbose at Line 408. This could expose secrets in logcat output. Consider redacting sensitive headers.

🤖 Fix all issues with AI agents
In `@OpenPanelSDK/src/main/java/com/dev/openpanelsdk/OpenPanel.kt`:
- Around line 56-60: The getUserAgent() implementation currently creates a
WebView via WebView(context) to read settings.userAgentString which leaks native
resources; replace that WebView usage with
WebSettings.getDefaultUserAgent(context) (available since API 17, safe given
minSdk 24) and keep the existing fallback to System.getProperty("http.agent") so
getUserAgent() first tries WebSettings.getDefaultUserAgent(context) inside
runCatching and then .getOrNull() ?: runCatching {
System.getProperty("http.agent") }.getOrNull(); ensure no WebView is
instantiated anywhere in getUserAgent().
🧹 Nitpick comments (2)
OpenPanelSDK/src/main/java/com/dev/openpanelsdk/OpenPanel.kt (2)

46-53: Dead code: private isAndroidEnvironment() in companion object is never called.

This method is unreferenced. If it's intended for future use, consider removing it until needed (YAGNI), or wire it into the public instance method on Line 262.


264-268: logVerbose in OpenPanel is defined but never called.

The verbose flag is properly threaded through to Api, but OpenPanel's own logVerbose is unused. Consider adding verbose logging to send(), getUserAgent(), or init to make the feature useful at the SDK level, or remove it until needed.

Comment on lines +56 to +60
private fun getUserAgent(): String? = runCatching {
WebView(context).settings.userAgentString
}.getOrNull() ?: runCatching {
System.getProperty("http.agent")
}.getOrNull()
Copy link

@coderabbitai coderabbitai bot Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🌐 Web query:

Android WebSettings.getDefaultUserAgent static method API level documentation

💡 Result:

WebSettings.getDefaultUserAgent(Context context) is a static method in android.webkit.WebSettings that returns the default User-Agent string used by a WebView. It was added in API level 17 (Android 4.2). An individual WebView instance may use a different UA if you override it via WebSettings.setUserAgentString(String). [1][2]

Docs (API reference): [2]

Signature

  • Java: public static String getDefaultUserAgent (Context context) [2]
  • Kotlin: open static fun getDefaultUserAgent(context: Context!): String! [1]

🏁 Script executed:

cat -n OpenPanelSDK/src/main/java/com/dev/openpanelsdk/OpenPanel.kt | sed -n '50,75p'

Repository: Openpanel-dev/kotlin-sdk

Length of output: 1045


🏁 Script executed:

fd -e gradle -e properties | head -20

Repository: Openpanel-dev/kotlin-sdk

Length of output: 85


🏁 Script executed:

find . -name "build.gradle*" -o -name "gradle.properties" | xargs grep -l "minSdkVersion\|compileSdk" 2>/dev/null | head -5

Repository: Openpanel-dev/kotlin-sdk

Length of output: 122


🏁 Script executed:

cat OpenPanelSDK/build.gradle.kts | grep -i "minsdk\|compilesdk\|targetversion" -A 1

Repository: Openpanel-dev/kotlin-sdk

Length of output: 112


🏁 Script executed:

cat app/build.gradle.kts | grep -i "minsdk\|compilesdk\|targetversion" -A 1

Repository: Openpanel-dev/kotlin-sdk

Length of output: 134


Replace WebView(context) instantiation with WebSettings.getDefaultUserAgent(context) to avoid resource leak.

The current code instantiates a full WebView instance just to retrieve the user-agent string, and the instance is never destroyed — this leaks native resources. WebSettings.getDefaultUserAgent(context) is a static method (available since API 17) that returns the default UA string without allocating a WebView. Since the SDK's minSdk is 24, this is fully compatible.

Proposed fix
-    private fun getUserAgent(): String? = runCatching {
-        WebView(context).settings.userAgentString
-    }.getOrNull() ?: runCatching {
+    private fun getUserAgent(): String? = runCatching {
+        WebSettings.getDefaultUserAgent(context)
+    }.getOrNull() ?: runCatching {
         System.getProperty("http.agent")
     }.getOrNull()
🤖 Prompt for AI Agents
In `@OpenPanelSDK/src/main/java/com/dev/openpanelsdk/OpenPanel.kt` around lines 56
- 60, The getUserAgent() implementation currently creates a WebView via
WebView(context) to read settings.userAgentString which leaks native resources;
replace that WebView usage with WebSettings.getDefaultUserAgent(context)
(available since API 17, safe given minSdk 24) and keep the existing fallback to
System.getProperty("http.agent") so getUserAgent() first tries
WebSettings.getDefaultUserAgent(context) inside runCatching and then
.getOrNull() ?: runCatching { System.getProperty("http.agent") }.getOrNull();
ensure no WebView is instantiated anywhere in getUserAgent().

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is reasonable suggestion

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yzheka, glad you find it helpful! This change will prevent native resource leaks while still providing the same functionality. Let me know if you need any assistance with the implementation.

(\_/)
( •_•)
/ > 🔧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant