-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
41 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 27 additions & 26 deletions
53
library/src/main/java/tr/com/emrememis/device/info/library/DeviceInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |