From 524cf91ee0baefed52d03767b8bd841d0af504eb Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 13 Feb 2019 18:21:07 +0200 Subject: [PATCH] Add check for country dial code initial commit --- OsmAnd-telegram/res/values/strings.xml | 208 ++++++++++++++++++ .../osmand/telegram/ui/LoginDialogFragment.kt | 63 +++++- .../osmand/telegram/utils/OsmandApiUtils.kt | 5 + 3 files changed, 271 insertions(+), 5 deletions(-) diff --git a/OsmAnd-telegram/res/values/strings.xml b/OsmAnd-telegram/res/values/strings.xml index e55a47fba0..000347d4cb 100644 --- a/OsmAnd-telegram/res/values/strings.xml +++ b/OsmAnd-telegram/res/values/strings.xml @@ -1,4 +1,212 @@ + + 32,BE + 501,BZ + 229,BJ + 975,BT + 591,BO + 387,BA + 267,BW + 55,BR + 673,BN + 359,BG + 226,BF + 95,MM + 257,BI + 855,KH + 237,CM + 1,CA + 238,CV + 236,CF + 235,TD + 56,CL + 86,CN + 61,CX + 61,CC + 57,CO + 269,KM + 242,CG + 243,CD + 682,CK + 506,CR + 385,HR + 53,CU + 357,CY + 93,AF + 355,AL + 213,DZ + 376,AD + 244,AO + 672,AQ + 54,AR + 374,AM + 297,AW + 61,AU + 43,AT + 994,AZ + 973,BH + 880,BD + 375,BY + 420,CZ + 45,DK + 253,DJ + 670,TL + 593,EC + 20,EG + 503,SV + 240,GQ + 358,FI + 33,FR + 291,ER + 372,EE + 251,ET + 500,FK + 298,FO + 679,FJ + 689,PF + 241,GA + 220,GM + 995,GE + 49,DE + 233,GH + 350,GI + 30,GR + 299,GL + 502,GT + 224,GN + 245,GW + 592,GY + 509,HT + 504,HN + 852,HK + 36,HU + 91,IN + 62,ID + 98,IR + 964,IQ + 353,IE + 44,IM + 972,IL + 39,IT + 225,CI + 81,JP + 962,JO + 7,KZ + 254,KE + 686,KI + 965,KW + 996,KG + 856,LA + 371,LV + 961,LB + 266,LS + 231,LR + 218,LY + 423,LI + 370,LT + 352,LU + 853,MO + 389,MK + 261,MG + 265,MW + 60,MY + 960,MV + 223,ML + 356,MT + 692,MH + 222,MR + 230,MU + 262,YT + 52,MX + 691,FM + 373,MD + 377,MC + 976,MN + 382,ME + 212,MA + 258,MZ + 264,NA + 674,NR + 977,NP + 31,NL + 599,AN + 687,NC + 64,NZ + 505,NI + 227,NE + 234,NG + 683,NU + 850,KP + 47,NO + 968,OM + 92,PK + 680,PW + 507,PA + 675,PG + 595,PY + 51,PE + 63,PH + 870,PN + 48,PL + 351,PT + 1,PR + 974,QA + 40,RO + 7,RU + 250,RW + 590,BL + 685,WS + 378,SM + 239,ST + 966,SA + 221,SN + 381,RS + 248,SC + 232,SL + 65,SG + 421,SK + 386,SI + 677,SB + 252,SO + 27,ZA + 82,KR + 34,ES + 94,LK + 290,SH + 508,PM + 249,SD + 597,SR + 268,SZ + 46,SE + 41,CH + 963,SY + 886,TW + 992,TJ + 255,TZ + 66,TH + 228,TG + 690,TK + 676,TO + 216,TN + 90,TR + 993,TM + 688,TV + 971,AE + 256,UG + 44,GB + 380,UA + 598,UY + 1,US + 998,UZ + 678,VU + 39,VA + 58,VE + 84,VN + 681,WF + 967,YE + 260,ZM + 263,ZW + Telegram We use a Telegram (messaging app) to connect people and make it easier for you to communicate with them. Telegram open platform and OsmAnd Tracker is one of the customers. Your contacts can use any other Telegram client. diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LoginDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LoginDialogFragment.kt index 72ef7bc5af..92d00c279e 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LoginDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LoginDialogFragment.kt @@ -1,5 +1,6 @@ package net.osmand.telegram.ui +import android.annotation.SuppressLint import android.app.Dialog import android.content.DialogInterface import android.content.Intent @@ -25,8 +26,12 @@ import android.widget.ImageView import android.widget.LinearLayout import android.widget.TextView import net.osmand.PlatformUtil +import net.osmand.data.LatLon import net.osmand.telegram.R +import net.osmand.telegram.utils.AndroidNetworkUtils import net.osmand.telegram.utils.AndroidUtils +import net.osmand.telegram.utils.OsmandApiUtils +import org.json.JSONObject import studio.carbonylgroup.textfieldboxes.ExtendedEditText @@ -35,7 +40,7 @@ class LoginDialogFragment : BaseDialogFragment() { companion object { private const val TAG = "LoginDialogFragment" - private val LOG = PlatformUtil.getLog(LoginDialogFragment::class.java) + private val log = PlatformUtil.getLog(LoginDialogFragment::class.java) private const val LOGIN_DIALOG_TYPE_PARAM_KEY = "login_dialog_type_param" private const val SHOW_PROGRESS_PARAM_KEY = "show_progress_param" @@ -51,6 +56,8 @@ class LoginDialogFragment : BaseDialogFragment() { private var softKeyboardShown: Boolean = false + private var countryPhoneCode: String = "" + fun showWelcomeDialog(fragmentManager: FragmentManager) { welcomeDialogShown = true showDialog(fragmentManager, welcomeDialog = true) @@ -85,7 +92,7 @@ class LoginDialogFragment : BaseDialogFragment() { fragment.updateDialog(loginDialogType, showWelcomeDialog) } } catch (e: RuntimeException) { - LOG.error(e) + log.error(e) } } @@ -188,6 +195,7 @@ class LoginDialogFragment : BaseDialogFragment() { } } + @SuppressLint("SetTextI18n") @Suppress("DEPRECATION") private fun buildDialog(view: View?) { if (showWelcomeDialog) { @@ -248,7 +256,7 @@ class LoginDialogFragment : BaseDialogFragment() { val editText: ExtendedEditText? = layout.findViewById(t.editorId) if (editText != null && !showWelcomeDialog) { if (loginDialogActiveType == LoginDialogType.ENTER_PHONE_NUMBER) { - editText.setText("+") + editText.setText("+$countryPhoneCode") } editText.setOnEditorActionListener { _, actionId, _ -> if (actionId == EditorInfo.IME_ACTION_DONE) { @@ -266,10 +274,10 @@ class LoginDialogFragment : BaseDialogFragment() { override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} override fun afterTextChanged(s: Editable) { - changeContinueButtonEnabled(s.length > 1) + changeContinueButtonEnabled(s.length > countryPhoneCode.length + 1) } }) - changeContinueButtonEnabled(editText.text.length > 1) + changeContinueButtonEnabled(editText.text.length > countryPhoneCode.length + 1) editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F) } @@ -430,6 +438,51 @@ class LoginDialogFragment : BaseDialogFragment() { } } + override fun onStart() { + super.onStart() + checkCountryPhoneCode() + } + + private fun checkCountryPhoneCode() { + OsmandApiUtils.getLocationByIp(app, object : AndroidNetworkUtils.OnRequestResultListener { + override fun onResult(result: String?) { + if (result != null) { + try { + val obj = JSONObject(result) + val latitude = obj.getDouble("latitude") + val longitude = obj.getDouble("longitude") + val countryId = obj.getString("country_code") + if (latitude == 0.0 && longitude == 0.0) { + log.debug("Empty location") + } else { + val location = LatLon(latitude, longitude) + log.debug("$location") + countryPhoneCode = getCountryDialCode(countryId) + log.debug(countryPhoneCode) + } + } catch (e: Exception) { + log.debug("JSON parsing error: ", e) + } + } else { + log.debug("Empty response") + } + } + }) + } + + fun getCountryDialCode(countryId: String): String { + var countryDialCode = "" + val arrCountryCode = this.resources.getStringArray(R.array.DialingCountryCode) + arrCountryCode.forEach { + val arrDial = it.split(",") + if (arrDial[1].trim() == (countryId)) { + countryDialCode = arrDial[0] + return countryDialCode + } + } + return countryDialCode + } + private fun applyAuthParam(t: LoginDialogType, value: String) { getMainActivity()?.applyAuthParam(this, t, value) } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt index fe58650860..6c0ee5054c 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandApiUtils.kt @@ -11,6 +11,7 @@ import org.json.JSONObject const val BASE_URL = "https://live.osmand.net" const val BASE_SHARING_URL = "http://osmand.net/go" +const val GEO_IP_URL = "https://osmand.net/api/geo-ip" object OsmandApiUtils { @@ -43,6 +44,10 @@ object OsmandApiUtils { } } + fun getLocationByIp(app: TelegramApplication, listener: AndroidNetworkUtils.OnRequestResultListener) { + AndroidNetworkUtils.sendRequestAsync(app, GEO_IP_URL, null, "get location by IP", false, false, listener) + } + fun parseDeviceBot(deviceJSON: JSONObject): TelegramSettings.DeviceBot? { return try { TelegramSettings.DeviceBot().apply {