Telegram: Fixed minor ui issues. Added simple auth error handling.

This commit is contained in:
crimean 2018-06-06 14:06:56 +03:00
parent 4606aa0f7c
commit a28358992e
6 changed files with 100 additions and 18 deletions

View file

@ -2,10 +2,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.osmand.telegram"> package="net.osmand.telegram">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application <application
android:name="net.osmand.telegram.TelegramApplication"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
@ -13,15 +15,19 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="unspecified" android:screenOrientation="unspecified"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme" android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize"> android:windowSoftInputMode="adjustResize">
<activity android:name=".MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
</application> </application>

View file

@ -1,5 +1,6 @@
package net.osmand.telegram package net.osmand.telegram
import android.content.DialogInterface
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.DialogFragment import android.support.v4.app.DialogFragment
import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentManager
@ -44,7 +45,9 @@ class LoginDialogFragment : DialogFragment() {
} }
fun dismiss(fragmentManager: FragmentManager) { fun dismiss(fragmentManager: FragmentManager) {
getFragment(fragmentManager)?.dismiss() val loginDialogFragment = getFragment(fragmentManager)
loginDialogFragment?.dismissedManually = true
loginDialogFragment?.dismiss()
} }
private fun getFragment(fragmentManager: FragmentManager): LoginDialogFragment? { private fun getFragment(fragmentManager: FragmentManager): LoginDialogFragment? {
@ -54,6 +57,8 @@ class LoginDialogFragment : DialogFragment() {
private var loginDialogActiveTypes: Set<LoginDialogType>? = null private var loginDialogActiveTypes: Set<LoginDialogType>? = null
private var dismissedManually = false
enum class LoginDialogType(val paramKey: String, val viewId: Int, val editorId: Int) { enum class LoginDialogType(val paramKey: String, val viewId: Int, val editorId: Int) {
ENTER_PHONE_NUMBER(ENTER_PHONE_NUMBER_PARAM_KEY, R.id.enterPhoneNumberLayout, R.id.phoneNumberEditText), ENTER_PHONE_NUMBER(ENTER_PHONE_NUMBER_PARAM_KEY, R.id.enterPhoneNumberLayout, R.id.phoneNumberEditText),
ENTER_CODE(ENTER_CODE_PARAM_KEY, R.id.enterCodeLayout, R.id.codeEditText), ENTER_CODE(ENTER_CODE_PARAM_KEY, R.id.enterCodeLayout, R.id.codeEditText),
@ -89,6 +94,13 @@ class LoginDialogFragment : DialogFragment() {
return view return view
} }
override fun onDismiss(dialog: DialogInterface?) {
super.onDismiss(dialog)
if (!dismissedManually) {
getMainActivity()?.closeTelegram()
}
}
private fun buildDialog(view: View?) { private fun buildDialog(view: View?) {
val loginDialogActiveTypes = this.loginDialogActiveTypes val loginDialogActiveTypes = this.loginDialogActiveTypes
var hasProgress = false var hasProgress = false
@ -145,7 +157,7 @@ class LoginDialogFragment : DialogFragment() {
} }
val cancelButton: Button? = view?.findViewById(R.id.calcelButton) val cancelButton: Button? = view?.findViewById(R.id.calcelButton)
cancelButton?.setOnClickListener { cancelButton?.setOnClickListener {
getMainActivity()?.close() dismiss()
} }
} }

View file

@ -1,7 +1,6 @@
package net.osmand.telegram package net.osmand.telegram
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
@ -18,17 +17,21 @@ class MainActivity : AppCompatActivity(), TelegramListener {
private const val PROGRESS_MENU_ID = 2 private const val PROGRESS_MENU_ID = 2
} }
private val telegramHelper: TelegramHelper = TelegramHelper.instance
private var authParamRequestHandler: AuthParamRequestHandler? = null private var authParamRequestHandler: AuthParamRequestHandler? = null
private var paused: Boolean = false private var paused: Boolean = false
private val app: TelegramApplication
get() = application as TelegramApplication
private val telegramHelper: TelegramHelper
get() = app.telegramHelper
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
paused = false paused = false
telegramHelper.appDir = filesDir.absolutePath
authParamRequestHandler = telegramHelper.setAuthParamRequestHandler(object : AuthParamRequestListener { authParamRequestHandler = telegramHelper.setAuthParamRequestHandler(object : AuthParamRequestListener {
override fun onRequestAuthParam(paramType: AuthParamType) { override fun onRequestAuthParam(paramType: AuthParamType) {
runOnUiThread { runOnUiThread {
@ -37,6 +40,14 @@ class MainActivity : AppCompatActivity(), TelegramListener {
} }
} }
} }
override fun onAuthRequestError(code: Int, message: String) {
runOnUiThread {
if (!paused) {
Toast.makeText(this@MainActivity, "$code - $message", Toast.LENGTH_LONG).show()
}
}
}
}) })
telegramHelper.listener = this telegramHelper.listener = this
telegramHelper.init() telegramHelper.init()
@ -45,7 +56,9 @@ class MainActivity : AppCompatActivity(), TelegramListener {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
paused = false paused = false
invalidateOptionsMenu() invalidateOptionsMenu()
updateTitle()
} }
override fun onPause() { override fun onPause() {
@ -69,7 +82,7 @@ class MainActivity : AppCompatActivity(), TelegramListener {
} }
} }
fun logout(silent: Boolean = false) { fun logoutTelegram(silent: Boolean = false) {
if (telegramHelper.getAuthState() == AuthState.READY) { if (telegramHelper.getAuthState() == AuthState.READY) {
telegramHelper.logout() telegramHelper.logout()
} else { } else {
@ -81,7 +94,7 @@ class MainActivity : AppCompatActivity(), TelegramListener {
} }
} }
fun close() { fun closeTelegram() {
telegramHelper.close() telegramHelper.close()
} }
@ -92,7 +105,7 @@ class MainActivity : AppCompatActivity(), TelegramListener {
true true
} }
LOGOUT_MENU_ID -> { LOGOUT_MENU_ID -> {
logout() logoutTelegram()
true true
} }
else -> super.onOptionsItemSelected(item) else -> super.onOptionsItemSelected(item)
@ -159,9 +172,7 @@ class MainActivity : AppCompatActivity(), TelegramListener {
} }
fun applyAuthParam(loginDialogFragment: LoginDialogFragment?, loginDialogType: LoginDialogType, text: String) { fun applyAuthParam(loginDialogFragment: LoginDialogFragment?, loginDialogType: LoginDialogType, text: String) {
if (loginDialogFragment != null) { loginDialogFragment?.updateDialog(LoginDialogType.SHOW_PROGRESS)
loginDialogFragment.updateDialog(LoginDialogType.SHOW_PROGRESS)
}
when (loginDialogType) { when (loginDialogType) {
LoginDialogType.ENTER_PHONE_NUMBER -> authParamRequestHandler?.applyAuthParam(AuthParamType.PHONE_NUMBER, text) LoginDialogType.ENTER_PHONE_NUMBER -> authParamRequestHandler?.applyAuthParam(AuthParamType.PHONE_NUMBER, text)
LoginDialogType.ENTER_CODE -> authParamRequestHandler?.applyAuthParam(AuthParamType.CODE, text) LoginDialogType.ENTER_CODE -> authParamRequestHandler?.applyAuthParam(AuthParamType.CODE, text)

View file

@ -0,0 +1,50 @@
package net.osmand.telegram
import android.app.Application
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
class TelegramApplication : Application() {
val telegramHelper: TelegramHelper = TelegramHelper.instance
private val lastTimeInternetConnectionChecked: Long = 0
private var internetConnectionAvailable = true
override fun onCreate() {
super.onCreate()
telegramHelper.appDir = filesDir.absolutePath
}
val isWifiConnected: Boolean
get() {
val mgr = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val ni = mgr.activeNetworkInfo
return ni != null && ni.type == ConnectivityManager.TYPE_WIFI
}
private val isInternetConnected: Boolean
get() {
val mgr = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val active = mgr.activeNetworkInfo
if (active == null) {
return false
} else {
val state = active.state
return state != NetworkInfo.State.DISCONNECTED && state != NetworkInfo.State.DISCONNECTING
}
}
// Check internet connection available every 15 seconds
val isInternetConnectionAvailable: Boolean
get() = isInternetConnectionAvailable(false)
fun isInternetConnectionAvailable(update: Boolean): Boolean {
val delta = System.currentTimeMillis() - lastTimeInternetConnectionChecked
if (delta < 0 || delta > 15000 || update) {
internetConnectionAvailable = isInternetConnected
}
return internetConnectionAvailable
}
}

View file

@ -2,14 +2,12 @@ package net.osmand.telegram
import android.text.TextUtils import android.text.TextUtils
import net.osmand.telegram.TelegramHelper.AuthParamType.* import net.osmand.telegram.TelegramHelper.AuthParamType.*
import org.drinkless.td.libcore.telegram.Client import org.drinkless.td.libcore.telegram.Client
import org.drinkless.td.libcore.telegram.Client.ResultHandler import org.drinkless.td.libcore.telegram.Client.ResultHandler
import org.drinkless.td.libcore.telegram.TdApi import org.drinkless.td.libcore.telegram.TdApi
import org.drinkless.td.libcore.telegram.TdApi.AuthorizationState import org.drinkless.td.libcore.telegram.TdApi.AuthorizationState
import java.io.File import java.io.File
import java.util.TreeSet import java.util.*
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
class TelegramHelper private constructor() { class TelegramHelper private constructor() {
@ -50,6 +48,7 @@ class TelegramHelper private constructor() {
interface AuthParamRequestListener { interface AuthParamRequestListener {
fun onRequestAuthParam(paramType: AuthParamType) fun onRequestAuthParam(paramType: AuthParamType)
fun onAuthRequestError(code: Int, message: String)
} }
inner class AuthParamRequestHandler(val authParamRequestListener: AuthParamRequestListener) { inner class AuthParamRequestHandler(val authParamRequestListener: AuthParamRequestListener) {
@ -371,6 +370,8 @@ class TelegramHelper private constructor() {
when (obj.constructor) { when (obj.constructor) {
TdApi.Error.CONSTRUCTOR -> { TdApi.Error.CONSTRUCTOR -> {
log.error("Receive an error: $obj") log.error("Receive an error: $obj")
val errorObj = obj as TdApi.Error
authParamRequestHandler?.authParamRequestListener?.onAuthRequestError(errorObj.code, errorObj.message)
onAuthorizationStateUpdated(null) // repeat last action onAuthorizationStateUpdated(null) // repeat last action
} }
TdApi.Ok.CONSTRUCTOR -> { TdApi.Ok.CONSTRUCTOR -> {

View file

@ -2,7 +2,9 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:gravity="center"> android:gravity="center"
android:paddingRight="16dp"
android:paddingLeft="16dp">
<ProgressBar <ProgressBar
android:layout_width="32dp" android:layout_width="32dp"