From 92bb230b8f0caf0b11b0ad15f6655a99afd03848 Mon Sep 17 00:00:00 2001 From: memishood Date: Mon, 16 Nov 2020 22:19:32 +0300 Subject: [PATCH] update dependencies & refactor code --- example/build.gradle | 10 ++-- .../tr/com/emrememis/sample/MainActivity.kt | 15 ++---- library/build.gradle | 8 +-- .../device/info/library/DeviceInfo.kt | 53 ++++++++++--------- 4 files changed, 41 insertions(+), 45 deletions(-) diff --git a/example/build.gradle b/example/build.gradle index da403ce..784bbd9 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -34,10 +34,10 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation project(':library') - testImplementation 'junit:junit:4.13' - androidTestImplementation 'androidx.test.ext:junit:1.1.1' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + testImplementation 'junit:junit:4.13.1' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' } diff --git a/example/src/main/java/tr/com/emrememis/sample/MainActivity.kt b/example/src/main/java/tr/com/emrememis/sample/MainActivity.kt index 340892f..caa0918 100644 --- a/example/src/main/java/tr/com/emrememis/sample/MainActivity.kt +++ b/example/src/main/java/tr/com/emrememis/sample/MainActivity.kt @@ -5,27 +5,22 @@ import androidx.appcompat.app.AppCompatActivity import tr.com.emrememis.device.info.library.DeviceInfo class MainActivity : AppCompatActivity() { + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + val deviceInfo = DeviceInfo(this) - val imei = DeviceInfo.imei(this) - - if (imei != null) { + deviceInfo.imei?.let { imei -> // your code.. } - val simSerialNumber = DeviceInfo.simSerialNumber(this) - - if (simSerialNumber != null) { + deviceInfo.simSerialNumber?.let { simSerialNumber -> // your code.. } - val secureId = DeviceInfo.secureId(this) - - if (secureId != null) { + deviceInfo.secureId?.let { secureId -> // your code.. } - } } diff --git a/library/build.gradle b/library/build.gradle index bdda2f4..f0c4e49 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -37,9 +37,9 @@ android { dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation 'androidx.core:core-ktx:1.3.0' - implementation 'androidx.appcompat:appcompat:1.1.0' + implementation 'androidx.core:core-ktx:1.3.2' + implementation 'androidx.appcompat:appcompat:1.2.0' testImplementation 'junit:junit:4.13.1' - androidTestImplementation 'androidx.test.ext:junit:1.1.1' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' } \ No newline at end of file diff --git a/library/src/main/java/tr/com/emrememis/device/info/library/DeviceInfo.kt b/library/src/main/java/tr/com/emrememis/device/info/library/DeviceInfo.kt index c21575b..a85f997 100644 --- a/library/src/main/java/tr/com/emrememis/device/info/library/DeviceInfo.kt +++ b/library/src/main/java/tr/com/emrememis/device/info/library/DeviceInfo.kt @@ -1,7 +1,6 @@ package tr.com.emrememis.device.info.library import android.Manifest -import android.annotation.SuppressLint import android.content.Context import android.content.pm.PackageManager import android.os.Build @@ -9,35 +8,37 @@ import androidx.core.content.ContextCompat import android.telephony.TelephonyManager import android.provider.Settings -object DeviceInfo { - - @SuppressLint("HardwareIds", "MissingPermission") - fun imei(context: Context?): String? { - context ?: return null - val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager - if (hasPermissions(context)) { - return if (Build.VERSION.SDK_INT >= 26) telephonyManager.imei else telephonyManager.deviceId +@Suppress("DEPRECATION", "HardwareIds", "MissingPermission") +class DeviceInfo constructor(var context: Context?) { + val imei: String? + get() { + if (hasPermissions(context)) { + val telephonyManager = context?.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager? + return when { + Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> telephonyManager?.imei + else -> telephonyManager?.deviceId + } + } + return null } - return null - } + val simSerialNumber: String? + get() { + if (hasPermissions(context)) { + val telephonyManager = context?.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager? + return telephonyManager?.simSerialNumber + } + return null + } - @SuppressLint("MissingPermission", "HardwareIds") - fun simSerialNumber(context: Context?): String? { - context ?: return null - val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager - if (hasPermissions(context)) { - return telephonyManager.simSerialNumber + val secureId: String? + get() { + return Settings.Secure.getString(context?.contentResolver, Settings.Secure.ANDROID_ID) } - return null - } - @SuppressLint("HardwareIds") - fun secureId(context: Context?): String? { - context ?: return null - return Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID) + companion object { + fun hasPermissions(context: Context?): Boolean = context?.let { + ContextCompat.checkSelfPermission(it, Manifest.permission.READ_PHONE_STATE) + } == PackageManager.PERMISSION_GRANTED } - - private fun hasPermissions(context: Context): Boolean = ContextCompat.checkSelfPermission(context, - Manifest.permission.READ_PHONE_STATE)== PackageManager.PERMISSION_GRANTED } \ No newline at end of file