Telegram: Fixed minor ui issues. Added simple auth error handling.
This commit is contained in:
parent
4606aa0f7c
commit
a28358992e
6 changed files with 100 additions and 18 deletions
|
@ -2,10 +2,12 @@
|
|||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
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.WRITE_EXTERNAL_STORAGE" />
|
||||
|
||||
<application
|
||||
android:name="net.osmand.telegram.TelegramApplication"
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
|
@ -13,15 +15,19 @@
|
|||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:screenOrientation="unspecified"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme"
|
||||
android:windowSoftInputMode="adjustResize">
|
||||
android:theme="@style/AppTheme">
|
||||
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:configChanges="orientation|screenSize"
|
||||
android:windowSoftInputMode="adjustResize">
|
||||
|
||||
<activity android:name=".MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
|
||||
</activity>
|
||||
|
||||
</application>
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package net.osmand.telegram
|
||||
|
||||
import android.content.DialogInterface
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.DialogFragment
|
||||
import android.support.v4.app.FragmentManager
|
||||
|
@ -44,7 +45,9 @@ class LoginDialogFragment : DialogFragment() {
|
|||
}
|
||||
|
||||
fun dismiss(fragmentManager: FragmentManager) {
|
||||
getFragment(fragmentManager)?.dismiss()
|
||||
val loginDialogFragment = getFragment(fragmentManager)
|
||||
loginDialogFragment?.dismissedManually = true
|
||||
loginDialogFragment?.dismiss()
|
||||
}
|
||||
|
||||
private fun getFragment(fragmentManager: FragmentManager): LoginDialogFragment? {
|
||||
|
@ -54,6 +57,8 @@ class LoginDialogFragment : DialogFragment() {
|
|||
|
||||
private var loginDialogActiveTypes: Set<LoginDialogType>? = null
|
||||
|
||||
private var dismissedManually = false
|
||||
|
||||
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_CODE(ENTER_CODE_PARAM_KEY, R.id.enterCodeLayout, R.id.codeEditText),
|
||||
|
@ -89,6 +94,13 @@ class LoginDialogFragment : DialogFragment() {
|
|||
return view
|
||||
}
|
||||
|
||||
override fun onDismiss(dialog: DialogInterface?) {
|
||||
super.onDismiss(dialog)
|
||||
if (!dismissedManually) {
|
||||
getMainActivity()?.closeTelegram()
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildDialog(view: View?) {
|
||||
val loginDialogActiveTypes = this.loginDialogActiveTypes
|
||||
var hasProgress = false
|
||||
|
@ -145,7 +157,7 @@ class LoginDialogFragment : DialogFragment() {
|
|||
}
|
||||
val cancelButton: Button? = view?.findViewById(R.id.calcelButton)
|
||||
cancelButton?.setOnClickListener {
|
||||
getMainActivity()?.close()
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package net.osmand.telegram
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.Fragment
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
|
@ -18,17 +17,21 @@ class MainActivity : AppCompatActivity(), TelegramListener {
|
|||
private const val PROGRESS_MENU_ID = 2
|
||||
}
|
||||
|
||||
private val telegramHelper: TelegramHelper = TelegramHelper.instance
|
||||
private var authParamRequestHandler: AuthParamRequestHandler? = null
|
||||
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?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
paused = false
|
||||
|
||||
telegramHelper.appDir = filesDir.absolutePath
|
||||
authParamRequestHandler = telegramHelper.setAuthParamRequestHandler(object : AuthParamRequestListener {
|
||||
override fun onRequestAuthParam(paramType: AuthParamType) {
|
||||
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.init()
|
||||
|
@ -45,7 +56,9 @@ class MainActivity : AppCompatActivity(), TelegramListener {
|
|||
override fun onResume() {
|
||||
super.onResume()
|
||||
paused = false
|
||||
|
||||
invalidateOptionsMenu()
|
||||
updateTitle()
|
||||
}
|
||||
|
||||
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) {
|
||||
telegramHelper.logout()
|
||||
} else {
|
||||
|
@ -81,7 +94,7 @@ class MainActivity : AppCompatActivity(), TelegramListener {
|
|||
}
|
||||
}
|
||||
|
||||
fun close() {
|
||||
fun closeTelegram() {
|
||||
telegramHelper.close()
|
||||
}
|
||||
|
||||
|
@ -92,7 +105,7 @@ class MainActivity : AppCompatActivity(), TelegramListener {
|
|||
true
|
||||
}
|
||||
LOGOUT_MENU_ID -> {
|
||||
logout()
|
||||
logoutTelegram()
|
||||
true
|
||||
}
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
|
@ -159,9 +172,7 @@ class MainActivity : AppCompatActivity(), TelegramListener {
|
|||
}
|
||||
|
||||
fun applyAuthParam(loginDialogFragment: LoginDialogFragment?, loginDialogType: LoginDialogType, text: String) {
|
||||
if (loginDialogFragment != null) {
|
||||
loginDialogFragment.updateDialog(LoginDialogType.SHOW_PROGRESS)
|
||||
}
|
||||
loginDialogFragment?.updateDialog(LoginDialogType.SHOW_PROGRESS)
|
||||
when (loginDialogType) {
|
||||
LoginDialogType.ENTER_PHONE_NUMBER -> authParamRequestHandler?.applyAuthParam(AuthParamType.PHONE_NUMBER, text)
|
||||
LoginDialogType.ENTER_CODE -> authParamRequestHandler?.applyAuthParam(AuthParamType.CODE, text)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -2,14 +2,12 @@ package net.osmand.telegram
|
|||
|
||||
import android.text.TextUtils
|
||||
import net.osmand.telegram.TelegramHelper.AuthParamType.*
|
||||
|
||||
import org.drinkless.td.libcore.telegram.Client
|
||||
import org.drinkless.td.libcore.telegram.Client.ResultHandler
|
||||
import org.drinkless.td.libcore.telegram.TdApi
|
||||
import org.drinkless.td.libcore.telegram.TdApi.AuthorizationState
|
||||
|
||||
import java.io.File
|
||||
import java.util.TreeSet
|
||||
import java.util.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
class TelegramHelper private constructor() {
|
||||
|
@ -50,6 +48,7 @@ class TelegramHelper private constructor() {
|
|||
|
||||
interface AuthParamRequestListener {
|
||||
fun onRequestAuthParam(paramType: AuthParamType)
|
||||
fun onAuthRequestError(code: Int, message: String)
|
||||
}
|
||||
|
||||
inner class AuthParamRequestHandler(val authParamRequestListener: AuthParamRequestListener) {
|
||||
|
@ -371,6 +370,8 @@ class TelegramHelper private constructor() {
|
|||
when (obj.constructor) {
|
||||
TdApi.Error.CONSTRUCTOR -> {
|
||||
log.error("Receive an error: $obj")
|
||||
val errorObj = obj as TdApi.Error
|
||||
authParamRequestHandler?.authParamRequestListener?.onAuthRequestError(errorObj.code, errorObj.message)
|
||||
onAuthorizationStateUpdated(null) // repeat last action
|
||||
}
|
||||
TdApi.Ok.CONSTRUCTOR -> {
|
||||
|
|
|
@ -2,7 +2,9 @@
|
|||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center">
|
||||
android:gravity="center"
|
||||
android:paddingRight="16dp"
|
||||
android:paddingLeft="16dp">
|
||||
|
||||
<ProgressBar
|
||||
android:layout_width="32dp"
|
||||
|
|
Loading…
Reference in a new issue