DeviceInfoUtils

디바이스 데이터를 가져온다. 최상위 객체로 구현(클래스 아님)

getAppVersionCode

  • App의 버전코드를 가져온다.

fun Context.getAppVersionCode(): Int {
    return BuildConfig.VERSION_CODE
}

getAppVersionName

  • App의 버전이름을 가져온다.

fun Context.getAppVersionName(): String {
    return BuildConfig.VERSION_NAME
}

getAppOS

  • OS type을 반환한다.

fun Context.getAppOs(type:Int):String{
    return if (type == 0) "AOS" else "ANDROID"
}

getOsVersion

  • OS version을 반환한다.

fun Context.getOsVersion(): Int{
    return Build.VERSION.SDK_INT
}

getDeviceUuid

  • device의 uuid를 반환한다.

  • android 10의 경우 random uuid를 반환한다 -> 보안 이슈

@SuppressLint("MissingPermission")
fun Context.getDeviceUuid(): String{
    //권한체크 하고
    if(!getPhoneStatePermission()){
        //TODO 권한없을시 어떻게 할지
        return ""
    }

    val telephony = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    //TODO 10 이상 이슈로 변경 - 디바이스 아이디 사용 불가
    val result = UUID.randomUUID().toString()
    //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //telephony.imei
    //} //else { //oreo 이하
        //telephony.getDeviceId()
    //}

    return result

}

getTelecomName

  • 통신사 이름을 반환한다.

fun Context.getTelecomName(): String {
    val telephony = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    var TelecomName: String? = telephony.simOperatorName
    if (TelecomName != null) {
        TelecomName = TelecomName.replace(" ".toRegex(), "")
    } else {
        TelecomName = ""
    }
    return TelecomName
}

getNetworkTelecomName

  • 네트워크를 제공하는 통신사의 이름을 반환한다.

fun Context.getNetworkTelecomName(): String{
    val telephony = getSystemService(Context.TELEPHONY_SERVICE) as? TelephonyManager
    var NetworkTelecomName  = telephony?.networkOperatorName
    if(NetworkTelecomName != null){
        NetworkTelecomName = NetworkTelecomName.replace(" ", "")
    }else{
        NetworkTelecomName = ""
    }
    return NetworkTelecomName
}

getTelecomCode

  • 통신사 코드를 반환한다.

fun Context.getTelecomCode(): String {
    val telephony = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    var TelecomCode = telephony.simOperator
    if (TelecomCode != null) {
        TelecomCode = TelecomCode.replace(" ", "")
    } else {
        TelecomCode = ""
    }
    return TelecomCode

}

getDeviceModel

  • 기기정보를 반환한다.

fun Context.getDeviceModel(): String{
    //val model = Build.MODEL
    val reqString = (Build.MANUFACTURER
            + " " + Build.MODEL + " " + Build.VERSION.RELEASE
            + " " + Build.VERSION_CODES::class.java.fields[android.os.Build.VERSION.SDK_INT].name)

    /*
    var s = "Debug-infos:\n"
    s += "OS Version: " + System.getProperty("os.version") + "(" + android.os.Build.VERSION.INCREMENTAL + ")\n"
    s += "OS API Level: " + android.os.Build.VERSION.RELEASE + "(" + android.os.Build.VERSION.SDK_INT + ")\n"
    s += "Device: " + android.os.Build.DEVICE + "\n"
    s += "Model (and Product): " + android.os.Build.MODEL + " (" + android.os.Build.PRODUCT + ")\n"
    */

    return reqString
}

getPushToken

  • FCM push token을 반환한다.

fun Context.getPushToken(): String{
    return SharedPreferenceManager.getFcmToken()
}

getWidthAndHeight

  • 기기의 화면 크기를 반환한다.

fun Context.getWidthAndHeight(pxOrDp:String ,activity:Activity): Array<String>{
    val display = activity.windowManager.defaultDisplay
    val displayName = display.getName()  // minSdkVersion=17+
    Log.i(TAG, "displayName  = $displayName")

    // pixels, dpi
    val metrics = DisplayMetrics()
    activity.windowManager.defaultDisplay.getMetrics(metrics)
    val heightPixels = metrics.heightPixels
    val widthPixels = metrics.widthPixels

    val densityDpi = metrics.densityDpi
    val xdpi = metrics.xdpi
    val ydpi = metrics.ydpi

    Log.i(TAG, "widthPixels  = $widthPixels")
    Log.i(TAG, "heightPixels = $heightPixels")
    Log.i(TAG, "densityDpi   = $densityDpi")
    Log.i(TAG, "xdpi         = $xdpi")
    Log.i(TAG, "ydpi         = $ydpi")

    var result =
        if(pxOrDp == "px"){
            arrayOf(widthPixels.toString(), heightPixels.toString()) //width, height
        }else{
            arrayOf(xdpi.toString(), ydpi.toString())
        }

    return result
}

getUserPermission

  • 사용자가 승인한 권한을 체크하여 반환한다.

fun Context.getUserPermission():MutableMap<String, String>{
    val permMap = mutableMapOf<String, String>().apply {
        this["mic"] = "N"
        this["file"] = "N" //file read/write (photo)
        this["camera"] = "N"
        this["contact"] = "N"
        this["location"] = "N"
        this["phone"] = "N"
        this["push"] = "N"
        //...
    }

    //파일 읽기/쓰기 권한
    if(getReadCheckPermission() &&
        getWriteCheckPermission()){
        permMap["file"] = "Y"
    }
    //카메라권한체크
    if(getCameraPermission()){
        permMap["camera"] = "Y"
    }
    //연락처권한체크
    if(getContactPermission()){
        permMap["contact"] = "Y"
    }
    //위치권한
    if(getLocationCheckPermission()) {
        permMap["location"] = "Y"
    }
    //푸시권한은 설정은 이동해서 변경가능 확인만 한다.
    if (NotificationManagerCompat.from(this).areNotificationsEnabled()) {
        permMap["push"] = "Y"
    }
    //phone state 권한
    if (getPhoneStatePermission()) {
        permMap["phone"] = "Y"
    }

    //... 추가

    return permMap
}

Last updated

Was this helpful?