Skip to content

Commit

Permalink
2019-06-16 17:10
Browse files Browse the repository at this point in the history
Add function getOsClientVersion and getClientAgent
  • Loading branch information
Aryan-mor authored and Aryan-mor committed Jul 10, 2019
1 parent 5358b12 commit da0fda3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
2 changes: 1 addition & 1 deletion UtilsLibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
61 changes: 24 additions & 37 deletions UtilsLibrary/src/main/java/com/aryanmo/utils/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,14 +19,15 @@ 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




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) {
Expand All @@ -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
Expand All @@ -66,12 +53,13 @@ fun Activity.getVersionName(): String {

}

fun Activity.getAppInfo(): HashMap<String, String> {
fun Context.getAppInfo(): HashMap<String, String> {
try {
val hashMap = HashMap<String, String>()
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)
Expand All @@ -80,29 +68,28 @@ fun Activity.getAppInfo(): HashMap<String, String> {

}

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<String, String> {
try {
val hashMap = HashMap<String, String>()
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<String, String> {
val header = getAppInfo()
getDeviceId()?.let {
header.put("device_id", it)
}
return header
}

fun Context.getUserAgent(): String? {
return WebView(this).settings.userAgentString
}

fun Activity.closeApp() {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/aryanmo/utilsexample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class MainActivity : BaseActivity(true) {
// intentTo(TestActivity::class)


logE("deviceId -> " + getDeviceId())
getOsClientVersion().logThisE()
logE("userAgent -> " + getUserAgent())


val s = "MyString"
Expand Down

0 comments on commit da0fda3

Please sign in to comment.