-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use meta-data for user agent client (#439)
- Loading branch information
Showing
19 changed files
with
209 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,17 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
</manifest> | ||
<application> | ||
<!-- | ||
Meta-data tags added here to test challenges that may arise when merging multiple | ||
AndroidManifest.xml files with same meta-data tags. | ||
See AndroidManifest.xml in androidTest directory of core module for more details. | ||
--> | ||
<meta-data | ||
android:name="io.customer.sdk.android.core.SDK_SOURCE" | ||
android:value="CommonTestAndroidSDK" /> | ||
<meta-data | ||
android:name="io.customer.sdk.android.core.SDK_VERSION" | ||
android:value="0.1.0" /> | ||
</application> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<application> | ||
<!-- | ||
This is how wrapper SDKs can provide their own values for the SDK_SOURCE and SDK_VERSION meta-data. | ||
tools:replace="android:value" is used to replace the value of the meta-data. | ||
tools:node="replace" is used to replace the entire meta-data tag. | ||
Either of these attributes can be used to resolve any conflicts that may arise when merging | ||
multiple AndroidManifest.xml files with the same meta-data tags. | ||
--> | ||
<meta-data | ||
android:name="io.customer.sdk.android.core.SDK_SOURCE" | ||
android:value="TestUserAgent" | ||
tools:replace="android:value" /> | ||
<meta-data | ||
android:name="io.customer.sdk.android.core.SDK_VERSION" | ||
android:value="1.3.5" | ||
tools:node="replace" /> | ||
</application> | ||
</manifest> |
15 changes: 15 additions & 0 deletions
15
core/src/androidTest/java/io/customer/sdk/data/store/AndroidManifestClientTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.customer.sdk.data.store | ||
|
||
import io.customer.commontest.core.AndroidTest | ||
import io.customer.sdk.core.extensions.applicationMetaData | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.Test | ||
|
||
class AndroidManifestClientTest : AndroidTest() { | ||
@Test | ||
fun fromManifest_givenTestMetaData_expectClientWithTestMetaData() { | ||
val client = Client.fromMetadata(application.applicationMetaData()) | ||
|
||
client.toString() shouldBeEqualTo "TestUserAgent Client/1.3.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
core/src/main/kotlin/io/customer/sdk/core/extensions/ContextExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.customer.sdk.core.extensions | ||
|
||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import android.os.Build | ||
import android.os.Bundle | ||
import io.customer.sdk.core.di.SDKComponent | ||
|
||
/** | ||
* Retrieves application meta-data from AndroidManifest.xml file. | ||
* | ||
* @return The meta-data bundle from application info. | ||
*/ | ||
fun Context.applicationMetaData(): Bundle? = try { | ||
val applicationInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
packageManager.getApplicationInfo( | ||
packageName, | ||
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong()) | ||
) | ||
} else { | ||
@Suppress("DEPRECATION") | ||
packageManager.getApplicationInfo( | ||
packageName, | ||
PackageManager.GET_META_DATA | ||
) | ||
} | ||
applicationInfo.metaData | ||
} catch (ex: Exception) { | ||
SDKComponent.logger.error("Failed to get ApplicationInfo with error: ${ex.message}") | ||
null | ||
} |
88 changes: 25 additions & 63 deletions
88
core/src/main/kotlin/io/customer/sdk/data/store/Client.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,45 @@ | ||
package io.customer.sdk.data.store | ||
|
||
import android.os.Bundle | ||
import io.customer.sdk.Version | ||
|
||
/** | ||
* Sealed class to hold information about the SDK wrapper and package that the | ||
* client app is using. | ||
* Represents the client information to append with user-agent. | ||
* | ||
* @property source name of the client to append with user-agent. | ||
* @property sdkVersion version of the SDK used. | ||
*/ | ||
sealed class Client( | ||
@Suppress("MemberVisibilityCanBePrivate") | ||
class Client( | ||
val source: String, | ||
val sdkVersion: String | ||
) { | ||
override fun toString(): String = "$source Client/$sdkVersion" | ||
|
||
/** | ||
* Simpler class for Android clients. | ||
*/ | ||
class Android(sdkVersion: String) : Client(source = SOURCE_ANDROID, sdkVersion = sdkVersion) | ||
|
||
/** | ||
* Simpler class for ReactNative clients. | ||
*/ | ||
class ReactNative(sdkVersion: String) : Client( | ||
source = SOURCE_REACT_NATIVE, | ||
sdkVersion = sdkVersion | ||
) | ||
|
||
/** | ||
* Simpler class for Expo clients. | ||
*/ | ||
class Expo(sdkVersion: String) : Client(source = SOURCE_EXPO, sdkVersion = sdkVersion) | ||
|
||
/** | ||
* Simpler class for Flutter clients. | ||
*/ | ||
class Flutter(sdkVersion: String) : Client(source = SOURCE_FLUTTER, sdkVersion = sdkVersion) | ||
|
||
/** | ||
* Other class to allow adding custom sources for clients that are not | ||
* supported above. | ||
* <p/> | ||
* Use this only if the client platform is not available in the above list. | ||
*/ | ||
class Other internal constructor( | ||
source: String, | ||
sdkVersion: String | ||
) : Client(source = source, sdkVersion = sdkVersion) | ||
|
||
companion object { | ||
internal const val SOURCE_ANDROID = "Android" | ||
internal const val SOURCE_REACT_NATIVE = "ReactNative" | ||
internal const val SOURCE_EXPO = "Expo" | ||
internal const val SOURCE_FLUTTER = "Flutter" | ||
private const val SOURCE_ANDROID = "Android" | ||
internal const val META_DATA_SDK_SOURCE = "io.customer.sdk.android.core.SDK_SOURCE" | ||
internal const val META_DATA_SDK_VERSION = "io.customer.sdk.android.core.SDK_VERSION" | ||
|
||
/** | ||
* Helper method to create client from raw values | ||
* Creates a new [Client] instance from the manifest meta-data. | ||
* If the user-agent or SDK version is not found, the default client is returned. | ||
* Default client is created with [SOURCE_ANDROID] and SDK version mentioned in [Version] class. | ||
* | ||
* @param source raw string of client source (case insensitive) | ||
* @param sdkVersion version of the SDK used | ||
* @return [Client] created from provided values | ||
* @param metadata Android application meta-data to retrieve the user-agent and SDK version from. | ||
* @return The client instance created from the manifest meta-data. | ||
* If not found, the default client is returned. | ||
*/ | ||
fun fromRawValue(source: String, sdkVersion: String): Client = when { | ||
source.equals( | ||
other = SOURCE_ANDROID, | ||
ignoreCase = true | ||
) -> Android(sdkVersion = sdkVersion) | ||
source.equals( | ||
other = SOURCE_REACT_NATIVE, | ||
ignoreCase = true | ||
) -> ReactNative(sdkVersion = sdkVersion) | ||
source.equals( | ||
other = SOURCE_EXPO, | ||
ignoreCase = true | ||
) -> Expo(sdkVersion = sdkVersion) | ||
source.equals( | ||
other = SOURCE_FLUTTER, | ||
ignoreCase = true | ||
) -> Flutter(sdkVersion = sdkVersion) | ||
else -> Other(source = source, sdkVersion = sdkVersion) | ||
fun fromMetadata(metadata: Bundle?): Client { | ||
val userAgent = metadata?.getString(META_DATA_SDK_SOURCE) | ||
val sdkVersion = metadata?.getString(META_DATA_SDK_VERSION) | ||
|
||
// If either value is null or blank, return the default client | ||
return if (userAgent.isNullOrBlank() || sdkVersion.isNullOrBlank()) { | ||
Client(source = SOURCE_ANDROID, sdkVersion = Version.version) | ||
} else { | ||
Client(source = userAgent, sdkVersion = sdkVersion) | ||
} | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
core/src/test/java/io/customer/sdk/data/store/ClientTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package io.customer.sdk.data.store | ||
|
||
import android.os.Bundle | ||
import io.customer.commontest.core.RobolectricTest | ||
import io.customer.sdk.Version | ||
import org.amshove.kluent.shouldBeEqualTo | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class ClientTest : RobolectricTest() { | ||
private val defaultClientString: String = "Android Client/${Version.version}" | ||
|
||
private fun createMetadata( | ||
userAgent: String?, | ||
sdkVersion: String? | ||
) = Bundle().apply { | ||
userAgent?.let { putString(Client.META_DATA_SDK_SOURCE, it) } | ||
sdkVersion?.let { putString(Client.META_DATA_SDK_VERSION, it) } | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenValidMetaData_expectClientWithMetaData() { | ||
val metadata = createMetadata("ReactNative", "1.2.3") | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo "ReactNative Client/1.2.3" | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenNullUserAgent_expectDefaultSourceUsed() { | ||
val metadata = createMetadata(null, "1.2.3") | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenEmptyUserAgent_expectDefaultSourceUsed() { | ||
val metadata = createMetadata("", "1.2.3") | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenNullSdkVersion_expectDefaultSdkVersionUsed() { | ||
val metadata = createMetadata("ReactNative", null) | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenEmptySdkVersion_expectDefaultSdkVersionUsed() { | ||
val metadata = createMetadata("ReactNative", "") | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenNullMetaData_expectDefaultValuesUsed() { | ||
val metadata = createMetadata(null, null) | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
|
||
@Test | ||
fun fromManifest_givenEmptyMetaData_expectDefaultValuesUsed() { | ||
val metadata = createMetadata("", "") | ||
|
||
val client = Client.fromMetadata(metadata) | ||
|
||
client.toString() shouldBeEqualTo defaultClientString | ||
} | ||
} |
Oops, something went wrong.