From 490a9a2a251d608a3db6e9462e670e1c919e5395 Mon Sep 17 00:00:00 2001 From: Mygod Date: Thu, 14 Feb 2019 12:34:59 +0800 Subject: [PATCH] Handle private company for which there is no address nor country --- .../java/be/mygod/vpnhotspot/client/MacLookup.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt b/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt index cc93cdb5..5f7ed771 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/client/MacLookup.kt @@ -49,11 +49,10 @@ object MacLookup { val obj = JSONObject(response).getJSONObject("result") obj.optString("error", null)?.also { throw UnexpectedError(mac, it) } val company = obj.getString("company") - val match = obj.optString("country")?.let { countryCodeRegex.matchEntire(it) } - ?: obj.optString("address")?.let { countryCodeRegex.find(it) } + val match = extractCountry(mac, response, obj) val result = if (match != null) { String(match.groupValues[1].flatMap { listOf('\uD83C', it + 0xDDA5) }.toCharArray()) + ' ' + company - } else company.also { Timber.w(UnexpectedError(mac, response)) } + } else company AppDatabase.instance.clientRecordDao.upsert(mac) { nickname = result macLookupPending = false @@ -70,4 +69,13 @@ object MacLookup { } } } + + private fun extractCountry(mac: Long, response: String, obj: JSONObject): MatchResult? { + obj.optString("country")?.let { countryCodeRegex.matchEntire(it) }?.also { return it } + val address = obj.optString("address") + if (address.isNullOrBlank()) return null + countryCodeRegex.find(address)?.also { return it } + Timber.w(UnexpectedError(mac, response)) + return null + } }