Merge pull request #6548 from osmandapp/TelegramImprovements

Telegram login with country codes
This commit is contained in:
Alexey 2019-02-14 15:14:35 +03:00 committed by GitHub
commit 3d49967332
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 153 additions and 20 deletions

View file

@ -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
@ -12,6 +13,8 @@ import android.support.v4.app.FragmentManager
import android.support.v4.content.ContextCompat
import android.support.v7.widget.AppCompatImageView
import android.text.*
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.util.TypedValue
@ -26,7 +29,11 @@ import android.widget.LinearLayout
import android.widget.TextView
import net.osmand.PlatformUtil
import net.osmand.telegram.R
import net.osmand.telegram.utils.AndroidNetworkUtils
import net.osmand.telegram.utils.AndroidUtils
import net.osmand.telegram.utils.DataConstants
import net.osmand.telegram.utils.OsmandApiUtils
import org.json.JSONObject
import studio.carbonylgroup.textfieldboxes.ExtendedEditText
@ -35,7 +42,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 +58,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 +94,7 @@ class LoginDialogFragment : BaseDialogFragment() {
fragment.updateDialog(loginDialogType, showWelcomeDialog)
}
} catch (e: RuntimeException) {
LOG.error(e)
log.error(e)
}
}
@ -165,7 +174,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()
@ -188,6 +197,7 @@ class LoginDialogFragment : BaseDialogFragment() {
}
}
@SuppressLint("SetTextI18n")
@Suppress("DEPRECATION")
private fun buildDialog(view: View?) {
if (showWelcomeDialog) {
@ -247,8 +257,8 @@ class LoginDialogFragment : BaseDialogFragment() {
layout.visibility = View.VISIBLE
val editText: ExtendedEditText? = layout.findViewById(t.editorId)
if (editText != null && !showWelcomeDialog) {
if (loginDialogActiveType == LoginDialogType.ENTER_PHONE_NUMBER) {
editText.setText("+")
if (loginDialogActiveType == LoginDialogType.ENTER_PHONE_NUMBER && editText.text.isEmpty()) {
editText.setText(countryPhoneCode)
}
editText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
@ -262,14 +272,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 > 1)
changeContinueButtonEnabled(s.length > passTextLength)
}
})
changeContinueButtonEnabled(editText.text.length > 1)
changeContinueButtonEnabled(editText.text.length > passTextLength)
editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)
}
@ -281,8 +292,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)
}
@ -313,14 +325,31 @@ class LoginDialogFragment : BaseDialogFragment() {
if (loginDialogActiveType == LoginDialogType.PRIVACY_POLICY) {
titleView?.setTextColor(ContextCompat.getColor(app, R.color.text_bold_highlight))
val useTelegramDescr = getString(R.string.privacy_policy_use_telegram)
descriptionView?.text = SpannableString(useTelegramDescr).apply {
descriptionView?.apply {
text = SpannableString(useTelegramDescr).apply {
val telegram = getString(R.string.shared_string_telegram)
val start = useTelegramDescr.indexOf(telegram)
if (start != -1) {
val clickableSpan = object : ClickableSpan() {
override fun onClick(textView: View) {
context?.also { ctx ->
startActivity(AndroidUtils.getPlayMarketIntent(ctx, TELEGRAM_PACKAGE))
}
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
}
}
setSpan(clickableSpan, start, start + telegram.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
setSpan(ForegroundColorSpan(ContextCompat.getColor(app, R.color.text_bold_highlight)), start, start + telegram.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
setSpan(StyleSpan(android.graphics.Typeface.BOLD), start, start + telegram.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
movementMethod = LinkMovementMethod.getInstance()
}
view.findViewById<TextView>(R.id.privacy_policy_agree).apply {
val policyAgreeDescr = getString(R.string.privacy_policy_agree)
text = SpannableString(policyAgreeDescr).apply {
@ -328,13 +357,42 @@ class LoginDialogFragment : BaseDialogFragment() {
val osmAndPrivacyPolicy = getString(R.string.osmand_privacy_policy)
var start = policyAgreeDescr.indexOf(telegramPrivacyPolicy)
if (start != -1) {
val clickableSpanTelegram = object : ClickableSpan() {
override fun onClick(textView: View) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(TELEGRAM_PRIVACY_POLICY))
if (AndroidUtils.isIntentSafe(context, intent)) {
startActivity(intent)
}
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
}
}
setSpan(clickableSpanTelegram, start, start + telegramPrivacyPolicy.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
setSpan(ForegroundColorSpan(ContextCompat.getColor(app, R.color.text_bold_highlight)), start, start + telegramPrivacyPolicy.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
start = policyAgreeDescr.indexOf(osmAndPrivacyPolicy)
if (start != -1) {
val clickableSpanOsmAnd = object : ClickableSpan() {
override fun onClick(textView: View) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(OSMAND_PRIVACY_POLICY))
if (AndroidUtils.isIntentSafe(context, intent)) {
startActivity(intent)
}
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
}
}
setSpan(clickableSpanOsmAnd, start, start + osmAndPrivacyPolicy.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
setSpan(ForegroundColorSpan(ContextCompat.getColor(app, R.color.text_bold_highlight)), start, start + osmAndPrivacyPolicy.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
}
movementMethod = LinkMovementMethod.getInstance()
}
view.findViewById<LinearLayout>(R.id.telegram_privacy_policy_btn).apply {
findViewById<TextView>(R.id.telegram_privacy_policy_title).text = getText(R.string.telegram_privacy_policy)
@ -398,7 +456,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)
}
}
@ -413,8 +471,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)
}
@ -430,6 +489,29 @@ 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 countryId = obj.getString("country_code")
countryPhoneCode = DataConstants.countryPhoneCodes[countryId]?.split(",")?.firstOrNull() ?: "+"
} catch (e: Exception) {
log.error("JSON parsing error: ", e)
}
} else {
log.debug("Empty response")
}
}
})
}
private fun applyAuthParam(t: LoginDialogType, value: String) {
getMainActivity()?.applyAuthParam(this, t, value)
}
@ -469,7 +551,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
}
}

View file

@ -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
}

View file

@ -0,0 +1,47 @@
package net.osmand.telegram.utils
object DataConstants {
val countryPhoneCodes = mapOf("AB" to "+7840,+7940,+99544", "AF" to "+93", "AX" to "+35818",
"AL" to "+355","DZ" to "+213", "AS" to "+1684", "AD" to "+376", "AO" to "+244","AI" to "+1264",
"AG" to "+1268", "AR" to "+54", "AM" to "+374","AW" to "+297", "SH" to "+247", "AU" to "+61",
"AU" to "+672","AT" to "+43", "AZ" to "+994", "BS" to "+1242", "BH" to "+973","BD" to "+880",
"BB" to "+1246", "AG" to "+1268", "BY" to "+375","BE" to "+32", "BZ" to "+501", "BJ" to "+229",
"BM" to "+1441","BT" to "+975", "BO" to "+591", "BQ" to "+5997", "BA" to "+387","BW" to "+267",
"BR" to "+55", "IO" to "+246", "VG" to "+1284","BN" to "+673", "BG" to "+359", "BF" to "+226",
"MY" to "+95","BI" to "+257", "KH" to "+855", "CM" to "+237", "CA" to "+1","CV" to "+238",
"KY" to "+1345", "CF" to "+236", "TD" to "+235","CL" to "+56", "CN" to "+86", "CX" to "+61",
"CC" to "+61","CO" to "+57", "KM" to "+269", "CG" to "+242", "CD" to "+243","CK" to "+682",
"CR" to "+506", "CI" to "+225", "HR" to "+385","CU" to "+53", "CW" to "+5999", "CY" to "+357",
"CZ" to "+420","DK" to "+45", "DG" to "+246", "DJ" to "+253", "DM" to "+1767","DO" to "+1809,+1829,+1849",
"TL" to "+670", "EC" to "+593","EG" to "+20", "SV" to "+503", "GQ" to "+240", "ER" to "+291",
"EE" to "+372", "ET" to "+251", "FK" to "+500", "FO" to "+298","FJ" to "+679", "FI" to "+358",
"FR" to "+33", "GF" to "+594","PF" to "+689", "GA" to "+241", "GM" to "+220", "GE" to "+995",
"DE" to "+49", "GH" to "+233", "GI" to "+350", "GR" to "+30","GL" to "+299", "GD" to "+1473",
"GP" to "+590", "GU" to "+1671","GT" to "+502", "GG" to "+44", "GN" to "+224", "GW" to "+245",
"GY" to "+592", "HT" to "+509", "HN" to "+504", "HK" to "+852","HU" to "+36", "IS" to "+354",
"IN" to "+91", "ID" to "+62","IR" to "+98", "IQ" to "+964", "IE" to "+353", "IL" to "+972",
"IT" to "+39", "JM" to "+1876", "SJ" to "+4779", "JP" to "+81","JE" to "+44", "JO" to "+962",
"KZ" to "+76,+77", "KE" to "+254","KI" to "+686", "KP" to "+850", "KR" to "+82", "KW" to "+965",
"KG" to "+996", "LA" to "+856", "LV" to "+371", "LB" to "+961","LS" to "+266", "LR" to "+231",
"LY" to "+218", "LI" to "+423","LT" to "+370", "LU" to "+352", "MO" to "+853", "MK" to "+389",
"MG" to "+261", "MW" to "+265", "MY" to "+60", "MV" to "+960","ML" to "+223", "MT" to "+356",
"MH" to "+692", "MQ" to "+596","MR" to "+222", "MU" to "+230", "YT" to "+262", "MX" to "+52",
"FM" to "+691", "MD" to "+373", "MC" to "+377", "MN" to "+976","ME" to "+382", "MS" to "+1664",
"MA" to "+212", "MZ" to "+258","NA" to "+264", "NR" to "+674", "NP" to "+977", "NL" to "+31",
"NC" to "+687", "NZ" to "+64", "NI" to "+505", "NE" to "+227","NG" to "+234", "NU" to "+683",
"NF" to "+672", "MP" to "+1670","NO" to "+47", "OM" to "+968", "PK" to "+92", "PW" to "+680",
"PS" to "+970", "PA" to "+507", "PG" to "+675", "PY" to "+595","PE" to "+51", "PH" to "+63",
"PN" to "+64", "PL" to "+48","PT" to "+351", "PR" to "+1787,+1939", "QA" to "+974", "RE" to "+262",
"RO" to "+40", "RU" to "+7", "RW" to "+250", "BL" to "+590","SH" to "+290", "KN" to "+1869",
"LC" to "+1758", "MF" to "+590","PM" to "+508", "VC" to "+1784", "WS" to "+685", "SM" to "+378",
"ST" to "+239", "SA" to "+966", "SN" to "+221", "RS" to "+381","SC" to "+248", "SL" to "+232",
"SG" to "+65", "BQ" to "+5993","SX" to "+1721", "SK" to "+421", "SI" to "+386", "SB" to "+677",
"SO" to "+252", "ZA" to "+27", "GS" to "+500", "!1" to "+99534","SS" to "+211", "ES" to "+34",
"LK" to "+94", "SD" to "+249","SR" to "+597", "SJ" to "+4779", "SZ" to "+268", "SE" to "+46",
"CH" to "+41", "SY" to "+963", "TW" to "+886", "TJ" to "+992","TZ" to "+255", "TH" to "+66",
"TG" to "+228", "TK" to "+690","TO" to "+676", "TT" to "+1868", "TN" to "+216", "TR" to "+90",
"TM" to "+993", "TC" to "+1649", "TV" to "+688", "UG" to "+256","UA" to "+380", "AE" to "+971",
"UK" to "+44", "US" to "+1","UY" to "+598", "VI" to "+1340", "UZ" to "+998", "VU" to "+678", "VE" to "+58",
"VA" to "+3906698,+379", "VN" to "+84", "WF" to "+681","YE" to "+967", "ZM" to "+260", "ZW" to "+263")
}

View file

@ -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 {