Skip to content

Commit

Permalink
Align user agent with iOS version (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
kober32 authored Jun 23, 2023
1 parent a130757 commit c245372
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
5 changes: 4 additions & 1 deletion library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@
~ and limitations under the License.
-->

<manifest package="com.wultra.android.powerauth" />
<manifest package="com.wultra.android.powerauth"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import com.wultra.android.powerauth.networking.tokens.IPowerAuthTokenListener
import com.wultra.android.powerauth.networking.tokens.IPowerAuthTokenProvider
import com.wultra.android.powerauth.networking.tokens.TokenManager
import com.wultra.android.powerauth.networking.utils.AppUtils
import com.wultra.android.powerauth.networking.utils.ConnectionMonitor
import com.wultra.android.powerauth.networking.utils.getCurrentLocale
import com.wultra.android.powerauth.networking.utils.toBcp47LanguageTag
import io.getlime.security.powerauth.core.EciesCryptogram
import io.getlime.security.powerauth.core.EciesEncryptor
import io.getlime.security.powerauth.sdk.PowerAuthAuthentication
Expand Down Expand Up @@ -243,11 +243,17 @@ class UserAgent internal constructor(@PublishedApi internal val value: String? =
companion object {
fun libraryDefault(appContext: Context): UserAgent {
val appInfo = AppUtils.getMyPackageBasicInfo(appContext)
return UserAgent(
"PowerAuthNetworking/${BuildConfig.VERSION_NAME} " +
"(${Build.BRAND}; ${appContext.getCurrentLocale().toBcp47LanguageTag()}) " +
"${appInfo.packageName}/${appInfo.versionName}"
)
val product = "PowerAuthNetworking"
val sdkVer = BuildConfig.VERSION_NAME
val appVer = appInfo.versionName
val appId = appInfo.packageName
val lang = appContext.getCurrentLocale().language // we use here only language to fit iOS implementation
val maker = Build.BRAND
val os = "Android"
val osVer = Build.VERSION.RELEASE
val model = Build.MODEL
val network = ConnectionMonitor.getConnectivityStatus(appContext)
return UserAgent("$product/$sdkVer ($lang; $network) $appId/$appVer ($maker; $os/$osVer; $model)")
}

fun systemDefault() = UserAgent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package com.wultra.android.powerauth.networking.utils
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.*
import android.os.Build
import androidx.annotation.RequiresApi
import com.wultra.android.powerauth.networking.Logger

class AppUtils {
companion object {
Expand All @@ -34,4 +37,59 @@ class AppUtils {
return appContext.packageManager.getPackageInfo(appContext.packageName, flags)
}
}
}
}

object ConnectionMonitor {

enum class Status(val value: String) {
UNKNOWN("unknown"),
NO_CONNECTION("noConnection"),
WIFI("wifi"),
CELLULAR("cellular"),
WIRED("wired")
}

fun getConnectivityStatus(context: Context): String {
var status: Status
try {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
status = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getConnectionStatus(connectivityManager)
} else {
val activeNetworkInfo = connectivityManager.activeNetworkInfo
if (activeNetworkInfo != null && activeNetworkInfo.isConnected) {
getOlderConnectionStatus(activeNetworkInfo)
} else {
Status.UNKNOWN
}
}
} catch (e: Throwable) {
status = Status.UNKNOWN
Logger.d("Failed to create Connectivity Manager with Exception: $e")
}
return status.value
}

@RequiresApi(Build.VERSION_CODES.M)
private fun getConnectionStatus(connectivityManager: ConnectivityManager): Status {
val activeNetwork = connectivityManager.activeNetwork
val networkCapabilities = connectivityManager.getNetworkCapabilities(activeNetwork)

return when {
networkCapabilities == null -> Status.NO_CONNECTION
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> Status.WIFI
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> Status.CELLULAR
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> Status.WIRED
else -> Status.UNKNOWN
}
}

private fun getOlderConnectionStatus(networkInfo: NetworkInfo): Status {
return when (networkInfo.type) {
ConnectivityManager.TYPE_WIFI -> Status.WIFI
ConnectivityManager.TYPE_MOBILE -> Status.CELLULAR
ConnectivityManager.TYPE_ETHERNET -> Status.WIRED
else -> Status.UNKNOWN
}
}
}

0 comments on commit c245372

Please sign in to comment.