Skip to content

Commit

Permalink
Merge pull request #137 from nielsvanvelzen/normalize-android-name-ba…
Browse files Browse the repository at this point in the history
…ckport

Normalize the Android device name (backport)
  • Loading branch information
Maxr1998 authored Oct 24, 2020
2 parents 0b97de4 + a626bbe commit 9484987
Showing 1 changed file with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,23 @@ data class AndroidDevice(
override val deviceName: String
) : IDevice {
companion object {
private fun String.normalize() = replace("[^\\w\\s]".toRegex(), "")

@SuppressLint("HardwareIds")
fun getAutomaticId(context: Context): String =
Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)

fun getAutomaticName(context: Context): String {
// Use name from device settings
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
return Settings.Global.getString(context.contentResolver, Settings.Global.DEVICE_NAME)
return Settings.Global.getString(context.contentResolver, Settings.Global.DEVICE_NAME).normalize()
}

// Concatenate the name based on manufacturer and model
val manufacturer = Build.MANUFACTURER
val model = Build.MODEL
val manufacturer = Build.MANUFACTURER.normalize()
val model = Build.MODEL.normalize()

return if (model.startsWith(manufacturer)) model
return if (model.startsWith(manufacturer) || manufacturer.isBlank()) model
else "$manufacturer $model"
}

Expand Down

0 comments on commit 9484987

Please sign in to comment.