Skip to content

Commit

Permalink
update dependencies & refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
memishood committed Nov 16, 2020
1 parent 4526831 commit 92bb230
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 45 deletions.
10 changes: 5 additions & 5 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
15 changes: 5 additions & 10 deletions example/src/main/java/tr/com/emrememis/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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..
}

}
}
8 changes: 4 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
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
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
}

0 comments on commit 92bb230

Please sign in to comment.