From 6cd6f122991c68ac3fd10cfd18ef69cceb0474cb Mon Sep 17 00:00:00 2001 From: Chumva Date: Thu, 14 Feb 2019 12:48:37 +0200 Subject: [PATCH] Remove string-array and add map with countryPhoneCodes --- OsmAnd-telegram/res/values/strings.xml | 253 ------------------ .../osmand/telegram/ui/LoginDialogFragment.kt | 113 +++++--- .../telegram/ui/MyLocationTabFragment.kt | 11 +- 3 files changed, 84 insertions(+), 293 deletions(-) diff --git a/OsmAnd-telegram/res/values/strings.xml b/OsmAnd-telegram/res/values/strings.xml index df92fd08ea..e55a47fba0 100644 --- a/OsmAnd-telegram/res/values/strings.xml +++ b/OsmAnd-telegram/res/values/strings.xml @@ -1,257 +1,4 @@ - - AB,+7840,+7940,+99544 - AF,+93 - AX,+35818 - AL,+355 - DZ,+213 - AS,+1684 - AD,+376 - AO,+244 - AI,+1264 - AG,+1268 - AR,+54 - AM,+374 - AW,+297 - SH,+247 - AU,+61 - AU,+672 - AT,+43 - AZ,+994 - BS,+1242 - BH,+973 - BD,+880 - BB,+1246 - AG,+1268 - BY,+375 - BE,+32 - BZ,+501 - BJ,+229 - BM,+1441 - BT,+975 - BO,+591 - BQ,+5997 - BA,+387 - BW,+267 - BR,+55 - IO,+246 - VG,+1284 - BN,+673 - BG,+359 - BF,+226 - MY,+95 - BI,+257 - KH,+855 - CM,+237 - CA,+1 - CV,+238 - KY,+1345 - CF,+236 - TD,+235 - CL,+56 - CN,+86 - CX,+61 - CC,+61 - CO,+57 - KM,+269 - CG,+242 - CD,+243 - CK,+682 - CR,+506 - CI,+225 - HR,+385 - CU,+53 - CW,+5999 - CY,+357 - CZ,+420 - DK,+45 - DG,+246 - DJ,+253 - DM,+1767 - DO,+1809,+1829,+1849 - TL,+670 - EC,+593 - EG,+20 - SV,+503 - GQ,+240 - ER,+291 - EE,+372 - ET,+251 - FK,+500 - FO,+298 - FJ,+679 - FI,+358 - FR,+33 - GF,+594 - PF,+689 - GA,+241 - GM,+220 - GE,+995 - DE,+49 - GH,+233 - GI,+350 - GR,+30 - GL,+299 - GD,+1473 - GP,+590 - GU,+1671 - GT,+502 - GG,+44 - GN,+224 - GW,+245 - GY,+592 - HT,+509 - HN,+504 - HK,+852 - HU,+36 - IS,+354 - IN,+91 - ID,+62 - IR,+98 - IQ,+964 - IE,+353 - IL,+972 - IT,+39 - JM,+1876 - SJ,+4779 - JP,+81 - JE,+44 - JO,+962 - KZ,+76,+77 - KE,+254 - KI,+686 - KP,+850 - KR,+82 - KW,+965 - KG,+996 - LA,+856 - LV,+371 - LB,+961 - LS,+266 - LR,+231 - LY,+218 - LI,+423 - LT,+370 - LU,+352 - MO,+853 - MK,+389 - MG,+261 - MW,+265 - MY,+60 - MV,+960 - ML,+223 - MT,+356 - MH,+692 - MQ,+596 - MR,+222 - MU,+230 - YT,+262 - MX,+52 - FM,+691 - MD,+373 - MC,+377 - MN,+976 - ME,+382 - MS,+1664 - MA,+212 - MZ,+258 - NA,+264 - NR,+674 - NP,+977 - NL,+31 - NC,+687 - NZ,+64 - NI,+505 - NE,+227 - NG,+234 - NU,+683 - NF,+672 - MP,+1670 - NO,+47 - OM,+968 - PK,+92 - PW,+680 - PS,+970 - PA,+507 - PG,+675 - PY,+595 - PE,+51 - PH,+63 - PN,+64 - PL,+48 - PT,+351 - PR,+1787,+1939 - QA,+974 - RE,+262 - RO,+40 - RU,+7 - RW,+250 - BL,+590 - SH,+290 - KN,+1869 - LC,+1758 - MF,+590 - PM,+508 - VC,+1784 - WS,+685 - SM,+378 - ST,+239 - SA,+966 - SN,+221 - RS,+381 - SC,+248 - SL,+232 - SG,+65 - BQ,+5993 - SX,+1721 - SK,+421 - SI,+386 - SB,+677 - SO,+252 - ZA,+27 - GS,+500 - !1,+99534 - SS,+211 - ES,+34 - LK,+94 - SD,+249 - SR,+597 - SJ,+4779 - SZ,+268 - SE,+46 - CH,+41 - SY,+963 - TW,+886 - TJ,+992 - TZ,+255 - TH,+66 - TG,+228 - TK,+690 - TO,+676 - TT,+1868 - TN,+216 - TR,+90 - TM,+993 - TC,+1649 - TV,+688 - UG,+256 - UA,+380 - AE,+971 - UK,+44 - US,+1 - UY,+598 - VI,+1340 - UZ,+998 - VU,+678 - VE,+58 - VA,+3906698,+379 - VN,+84 - WF,+681 - YE,+967 - ZM,+260 - !1,+255 - ZW,+263 - 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 b50000eca8..3a71b30194 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LoginDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LoginDialogFragment.kt @@ -26,7 +26,6 @@ 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 @@ -56,6 +55,71 @@ class LoginDialogFragment : BaseDialogFragment() { private var softKeyboardShown: Boolean = false + private var countryPhoneCodes = mapOf( + Pair("AB", "+7840,+7940,+99544"), Pair("AF", "+93"), Pair("AX", "+35818"), Pair("AL", "+355"), + Pair("DZ", "+213"), Pair("AS", "+1684"), Pair("AD", "+376"), Pair("AO", "+244"), + Pair("AI", "+1264"), Pair("AG", "+1268"), Pair("AR", "+54"), Pair("AM", "+374"), + Pair("AW", "+297"), Pair("SH", "+247"), Pair("AU", "+61"), Pair("AU", "+672"), + Pair("AT", "+43"), Pair("AZ", "+994"), Pair("BS", "+1242"), Pair("BH", "+973"), + Pair("BD", "+880"), Pair("BB", "+1246"), Pair("AG", "+1268"), Pair("BY", "+375"), + Pair("BE", "+32"), Pair("BZ", "+501"), Pair("BJ", "+229"), Pair("BM", "+1441"), + Pair("BT", "+975"), Pair("BO", "+591"), Pair("BQ", "+5997"), Pair("BA", "+387"), + Pair("BW", "+267"), Pair("BR", "+55"), Pair("IO", "+246"), Pair("VG", "+1284"), + Pair("BN", "+673"), Pair("BG", "+359"), Pair("BF", "+226"), Pair("MY", "+95"), + Pair("BI", "+257"), Pair("KH", "+855"), Pair("CM", "+237"), Pair("CA", "+1"), + Pair("CV", "+238"), Pair("KY", "+1345"), Pair("CF", "+236"), Pair("TD", "+235"), + Pair("CL", "+56"), Pair("CN", "+86"), Pair("CX", "+61"), Pair("CC", "+61"), + Pair("CO", "+57"), Pair("KM", "+269"), Pair("CG", "+242"), Pair("CD", "+243"), + Pair("CK", "+682"), Pair("CR", "+506"), Pair("CI", "+225"), Pair("HR", "+385"), + Pair("CU", "+53"), Pair("CW", "+5999"), Pair("CY", "+357"), Pair("CZ", "+420"), + Pair("DK", "+45"), Pair("DG", "+246"), Pair("DJ", "+253"), Pair("DM", "+1767"), + Pair("DO", "+1809,+1829,+1849"), Pair("TL", "+670"), Pair("EC", "+593"), + Pair("EG", "+20"), Pair("SV", "+503"), Pair("GQ", "+240"), Pair("ER", "+291"), + Pair("EE", "+372"), Pair("ET", "+251"), Pair("FK", "+500"), Pair("FO", "+298"), + Pair("FJ", "+679"), Pair("FI", "+358"), Pair("FR", "+33"), Pair("GF", "+594"), + Pair("PF", "+689"), Pair("GA", "+241"), Pair("GM", "+220"), Pair("GE", "+995"), + Pair("DE", "+49"), Pair("GH", "+233"), Pair("GI", "+350"), Pair("GR", "+30"), + Pair("GL", "+299"), Pair("GD", "+1473"), Pair("GP", "+590"), Pair("GU", "+1671"), + Pair("GT", "+502"), Pair("GG", "+44"), Pair("GN", "+224"), Pair("GW", "+245"), + Pair("GY", "+592"), Pair("HT", "+509"), Pair("HN", "+504"), Pair("HK", "+852"), + Pair("HU", "+36"), Pair("IS", "+354"), Pair("IN", "+91"), Pair("ID", "+62"), + Pair("IR", "+98"), Pair("IQ", "+964"), Pair("IE", "+353"), Pair("IL", "+972"), + Pair("IT", "+39"), Pair("JM", "+1876"), Pair("SJ", "+4779"), Pair("JP", "+81"), + Pair("JE", "+44"), Pair("JO", "+962"), Pair("KZ", "+76,+77"), Pair("KE", "+254"), + Pair("KI", "+686"), Pair("KP", "+850"), Pair("KR", "+82"), Pair("KW", "+965"), + Pair("KG", "+996"), Pair("LA", "+856"), Pair("LV", "+371"), Pair("LB", "+961"), + Pair("LS", "+266"), Pair("LR", "+231"), Pair("LY", "+218"), Pair("LI", "+423"), + Pair("LT", "+370"), Pair("LU", "+352"), Pair("MO", "+853"), Pair("MK", "+389"), + Pair("MG", "+261"), Pair("MW", "+265"), Pair("MY", "+60"), Pair("MV", "+960"), + Pair("ML", "+223"), Pair("MT", "+356"), Pair("MH", "+692"), Pair("MQ", "+596"), + Pair("MR", "+222"), Pair("MU", "+230"), Pair("YT", "+262"), Pair("MX", "+52"), + Pair("FM", "+691"), Pair("MD", "+373"), Pair("MC", "+377"), Pair("MN", "+976"), + Pair("ME", "+382"), Pair("MS", "+1664"), Pair("MA", "+212"), Pair("MZ", "+258"), + Pair("NA", "+264"), Pair("NR", "+674"), Pair("NP", "+977"), Pair("NL", "+31"), + Pair("NC", "+687"), Pair("NZ", "+64"), Pair("NI", "+505"), Pair("NE", "+227"), + Pair("NG", "+234"), Pair("NU", "+683"), Pair("NF", "+672"), Pair("MP", "+1670"), + Pair("NO", "+47"), Pair("OM", "+968"), Pair("PK", "+92"), Pair("PW", "+680"), + Pair("PS", "+970"), Pair("PA", "+507"), Pair("PG", "+675"), Pair("PY", "+595"), + Pair("PE", "+51"), Pair("PH", "+63"), Pair("PN", "+64"), Pair("PL", "+48"), + Pair("PT", "+351"), Pair("PR", "+1787,+1939"), Pair("QA", "+974"), Pair("RE", "+262"), + Pair("RO", "+40"), Pair("RU", "+7"), Pair("RW", "+250"), Pair("BL", "+590"), + Pair("SH", "+290"), Pair("KN", "+1869"), Pair("LC", "+1758"), Pair("MF", "+590"), + Pair("PM", "+508"), Pair("VC", "+1784"), Pair("WS", "+685"), Pair("SM", "+378"), + Pair("ST", "+239"), Pair("SA", "+966"), Pair("SN", "+221"), Pair("RS", "+381"), + Pair("SC", "+248"), Pair("SL", "+232"), Pair("SG", "+65"), Pair("BQ", "+5993"), + Pair("SX", "+1721"), Pair("SK", "+421"), Pair("SI", "+386"), Pair("SB", "+677"), + Pair("SO", "+252"), Pair("ZA", "+27"), Pair("GS", "+500"), Pair("!1", "+99534"), + Pair("SS", "+211"), Pair("ES", "+34"), Pair("LK", "+94"), Pair("SD", "+249"), + Pair("SR", "+597"), Pair("SJ", "+4779"), Pair("SZ", "+268"), Pair("SE", "+46"), + Pair("CH", "+41"), Pair("SY", "+963"), Pair("TW", "+886"), Pair("TJ", "+992"), + Pair("TZ", "+255"), Pair("TH", "+66"), Pair("TG", "+228"), Pair("TK", "+690"), + Pair("TO", "+676"), Pair("TT", "+1868"), Pair("TN", "+216"), Pair("TR", "+90"), + Pair("TM", "+993"), Pair("TC", "+1649"), Pair("TV", "+688"), Pair("UG", "+256"), + Pair("UA", "+380"), Pair("AE", "+971"), Pair("UK", "+44"), Pair("US", "+1"), + Pair("UY", "+598"), Pair("VI", "+1340"), Pair("UZ", "+998"), Pair("VU", "+678"), + Pair("VE", "+58"), Pair("VA", "+3906698,+379"), Pair("VN", "+84"), Pair("WF", "+681"), + Pair("YE", "+967"), Pair("ZM", "+260"), Pair("ZW", "+263")) + private var countryPhoneCode: String = "+" fun showWelcomeDialog(fragmentManager: FragmentManager) { @@ -172,7 +236,7 @@ class LoginDialogFragment : BaseDialogFragment() { val width = if (expanded) ViewGroup.LayoutParams.MATCH_PARENT else ViewGroup.LayoutParams.WRAP_CONTENT params.apply { val horizontalMargin = app.resources.getDimensionPixelSize(if (acceptMode) R.dimen.dialog_welcome_padding_horizontal else R.dimen.content_padding_half) - setMargins(horizontalMargin, topMargin, horizontalMargin, AndroidUtils.dpToPx(context!!, bottomMargin)) + setMargins(horizontalMargin, topMargin, horizontalMargin, AndroidUtils.dpToPx(app, bottomMargin)) this.width = width } continueButton.requestLayout() @@ -270,14 +334,15 @@ class LoginDialogFragment : BaseDialogFragment() { AndroidUtils.softKeyboardDelayed(editText) focusRequested = true } + val passTextLength = if (loginDialogActiveType == LoginDialogType.ENTER_PHONE_NUMBER) countryPhoneCode.length else 0 editText.addTextChangedListener(object : TextWatcher { 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 > countryPhoneCode.length) + changeContinueButtonEnabled(s.length > passTextLength) } }) - changeContinueButtonEnabled(editText.text.length > countryPhoneCode.length) + changeContinueButtonEnabled(editText.text.length > passTextLength) editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F) } @@ -289,8 +354,9 @@ class LoginDialogFragment : BaseDialogFragment() { noTelegramViewContainer?.setOnClickListener { val focusedView = dialog.currentFocus - if (focusedView != null) { - AndroidUtils.hideSoftKeyboard(activity!!, focusedView) + val mainActivity = activity + if (focusedView != null && mainActivity != null) { + AndroidUtils.hideSoftKeyboard(mainActivity, focusedView) } updateDialog(LoginDialogType.GET_TELEGRAM, false) } @@ -406,7 +472,7 @@ class LoginDialogFragment : BaseDialogFragment() { val editText: ExtendedEditText? = layout.findViewById(t.editorId) val text = editText?.text.toString() if (!TextUtils.isEmpty(text) && text.length > 1) { - continueButton.setTextColor(ContextCompat.getColor(context!!, R.color.secondary_text_light)) + continueButton.setTextColor(ContextCompat.getColor(app, R.color.secondary_text_light)) applyAuthParam(t, text) } } @@ -421,8 +487,9 @@ class LoginDialogFragment : BaseDialogFragment() { LoginDialogType.ENTER_PHONE_NUMBER -> { showWelcomeDialog = true val focusedView = dialog.currentFocus - if (focusedView != null) { - AndroidUtils.hideSoftKeyboard(activity!!, focusedView) + val mainActivity = activity + if (focusedView != null && mainActivity != null) { + AndroidUtils.hideSoftKeyboard(mainActivity, focusedView) } buildDialog(view) } @@ -449,19 +516,10 @@ class LoginDialogFragment : BaseDialogFragment() { 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) - } + countryPhoneCode = countryPhoneCodes[countryId]?.split(",")?.firstOrNull() ?: "+" } catch (e: Exception) { - log.debug("JSON parsing error: ", e) + log.error("JSON parsing error: ", e) } } else { log.debug("Empty response") @@ -470,19 +528,6 @@ class LoginDialogFragment : BaseDialogFragment() { }) } - fun getCountryDialCode(countryId: String): String { - var countryDialCode = "" - val arrCountryCode = this.resources.getStringArray(R.array.DialingCountryCode) - arrCountryCode.forEach { - val arrDial = it.split(",") - if (arrDial[0].trim() == (countryId)) { - countryDialCode = arrDial[1] - return countryDialCode - } - } - return countryDialCode - } - private fun applyAuthParam(t: LoginDialogType, value: String) { getMainActivity()?.applyAuthParam(this, t, value) } @@ -522,7 +567,7 @@ class LoginDialogFragment : BaseDialogFragment() { private fun changeContinueButtonEnabled(enabled: Boolean) { if (enabled != continueButton.isEnabled) { val color = if (enabled) R.color.white else R.color.secondary_text_light - continueButton.setTextColor(ContextCompat.getColor(context!!, color)) + continueButton.setTextColor(ContextCompat.getColor(app, color)) continueButton.isEnabled = enabled } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt index cb22f4e849..df68fcd834 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt @@ -164,14 +164,14 @@ class MyLocationTabFragment : Fragment(), TelegramListener { if (Build.VERSION.SDK_INT >= 16) { layoutTransition.enableTransitionType(LayoutTransition.CHANGING) } - AndroidUtils.addStatusBarPadding19v(context!!, this) + AndroidUtils.addStatusBarPadding19v(app, this) title = findViewById(R.id.title) description = findViewById(R.id.description) } searchBoxBg = GradientDrawable().apply { shape = GradientDrawable.RECTANGLE - setColor(ContextCompat.getColor(context!!, R.color.screen_bg_light)) + setColor(ContextCompat.getColor(app, R.color.screen_bg_light)) cornerRadius = (searchBoxHeight / 2).toFloat() } @@ -662,8 +662,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener { val duration = shareInfo?.userSetLivePeriod if (duration != null && duration > 0) { - holder.descriptionDuration?.text = - OsmandFormatter.getFormattedDuration(context!!, duration) + holder.descriptionDuration?.text = OsmandFormatter.getFormattedDuration(app, duration) holder.description?.apply { visibility = View.VISIBLE text = "${getText(R.string.sharing_time)}:" @@ -676,7 +675,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener { val time = shareInfo?.additionalActiveTime ?: ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0] visibility = View.VISIBLE - text = "+ ${OsmandFormatter.getFormattedDuration(context!!, time)}" + text = "+ ${OsmandFormatter.getFormattedDuration(app, time)}" setOnClickListener { val expireTime = shareInfo?.getChatLiveMessageExpireTime() ?: 0 val newLivePeriod = expireTime + (shareInfo?.additionalActiveTime @@ -711,7 +710,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener { description.setSpan(CustomTypefaceSpan(typeface), start, description.length, 0) } description.setSpan(ForegroundColorSpan(ContextCompat.getColor(app, R.color.primary_text_light)), start, description.length, 0) - description.append((getString(R.string.in_time, OsmandFormatter.getFormattedDuration(context!!, expiresIn, true)))) + description.append((getString(R.string.in_time, OsmandFormatter.getFormattedDuration(app, expiresIn, true)))) text = description }