From da0fda31da1a7cda9635f79227724a77e3b3b113 Mon Sep 17 00:00:00 2001 From: Aryan-mor Date: Wed, 10 Jul 2019 12:29:35 +0430 Subject: [PATCH] 2019-06-16 17:10 Add function getOsClientVersion and getClientAgent --- UtilsLibrary/build.gradle | 2 +- .../java/com/aryanmo/utils/utils/Utils.kt | 61 ++++++++----------- .../com/aryanmo/utilsexample/MainActivity.kt | 3 + 3 files changed, 28 insertions(+), 38 deletions(-) diff --git a/UtilsLibrary/build.gradle b/UtilsLibrary/build.gradle index 602f460..40d6733 100644 --- a/UtilsLibrary/build.gradle +++ b/UtilsLibrary/build.gradle @@ -9,7 +9,7 @@ android { minSdkVersion 15 targetSdkVersion 28 versionCode 1 - versionName "1.3.11" + versionName "1.3.12" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/UtilsLibrary/src/main/java/com/aryanmo/utils/utils/Utils.kt b/UtilsLibrary/src/main/java/com/aryanmo/utils/utils/Utils.kt index 9f54dc3..c92efaa 100644 --- a/UtilsLibrary/src/main/java/com/aryanmo/utils/utils/Utils.kt +++ b/UtilsLibrary/src/main/java/com/aryanmo/utils/utils/Utils.kt @@ -9,6 +9,7 @@ import android.content.Intent import android.os.Build import android.os.VibrationEffect import android.os.Vibrator +import android.provider.Settings import android.text.Html import android.text.Spanned import android.util.TypedValue @@ -18,6 +19,7 @@ import com.aryanmo.utils.R import com.aryanmo.utils.utils.log.logError import com.aryanmo.utils.utils.log.logNullPointerExceptionError import java.util.* +import android.webkit.WebView @@ -25,7 +27,7 @@ import java.util.* val sdkApiLevel: Int get() = android.os.Build.VERSION.SDK_INT -fun Activity.getVersionCode(): Int { +fun Context.getVersionCode(): Int { try { val pInfo = this.packageManager.getPackageInfo(this.packageName, 0) return if (android.os.Build.VERSION.SDK_INT >= 28) { @@ -40,22 +42,7 @@ fun Activity.getVersionCode(): Int { return -1 } -fun Application.getVersionCode(): Int { - try { - val pInfo = this.packageManager.getPackageInfo(this.packageName, 0) - return if (android.os.Build.VERSION.SDK_INT >= 28) { - pInfo.longVersionCode.toInt() - } else { - pInfo.versionCode - } - } catch (e: Exception) { - logError("getVersionCode", e) - } - - return -1 -} - -fun Activity.getVersionName(): String { +fun Context.getVersionName(): String { try { val pInfo = this.packageManager.getPackageInfo(this.packageName, 0) return pInfo.versionName @@ -66,12 +53,13 @@ fun Activity.getVersionName(): String { } -fun Activity.getAppInfo(): HashMap { +fun Context.getAppInfo(): HashMap { try { val hashMap = HashMap() hashMap["os"] = "android" hashMap["android_sdk_api_level"] = sdkApiLevel.toString() - hashMap["version"] = this.getVersionCode().toString() + hashMap["version_code"] = this.getVersionCode().toString() + hashMap["version_name"] = this.getVersionName() return hashMap } catch (e: Exception) { logError("getAppInfo", e) @@ -80,29 +68,28 @@ fun Activity.getAppInfo(): HashMap { } -fun Application.getVersionName(): String { - try { - val pInfo = this.packageManager.getPackageInfo(this.packageName, 0) - return pInfo.versionName - } catch (e: Exception) { - logError("getVersionCode", e) - return "" +@SuppressLint("HardwareIds") +fun Context.getDeviceId(): String? { + return try { + Settings.Secure.getString( + contentResolver, + Settings.Secure.ANDROID_ID + ) + } catch (e: java.lang.Exception) { + null } - } -fun Application.getAppInfo(): HashMap { - try { - val hashMap = HashMap() - hashMap["os"] = "android" - hashMap["android_sdk_api_level"] = sdkApiLevel.toString() - hashMap["version"] = this.getVersionCode().toString() - return hashMap - } catch (e: Exception) { - logError("getAppInfo", e) - return HashMap() +fun Context.getOsClientVersion(): HashMap { + val header = getAppInfo() + getDeviceId()?.let { + header.put("device_id", it) } + return header +} +fun Context.getUserAgent(): String? { + return WebView(this).settings.userAgentString } fun Activity.closeApp() { diff --git a/app/src/main/java/com/aryanmo/utilsexample/MainActivity.kt b/app/src/main/java/com/aryanmo/utilsexample/MainActivity.kt index b983d66..0c2edd0 100644 --- a/app/src/main/java/com/aryanmo/utilsexample/MainActivity.kt +++ b/app/src/main/java/com/aryanmo/utilsexample/MainActivity.kt @@ -75,6 +75,9 @@ class MainActivity : BaseActivity(true) { // intentTo(TestActivity::class) + logE("deviceId -> " + getDeviceId()) + getOsClientVersion().logThisE() + logE("userAgent -> " + getUserAgent()) val s = "MyString"