diff --git a/OsmAnd-telegram/src/main/AndroidManifest.xml b/OsmAnd-telegram/src/main/AndroidManifest.xml index 512441075d..586867506b 100644 --- a/OsmAnd-telegram/src/main/AndroidManifest.xml +++ b/OsmAnd-telegram/src/main/AndroidManifest.xml @@ -2,10 +2,12 @@ + + android:theme="@style/AppTheme"> + + - + diff --git a/OsmAnd-telegram/src/main/java/net/osmand/telegram/LoginDialogFragment.kt b/OsmAnd-telegram/src/main/java/net/osmand/telegram/LoginDialogFragment.kt index 48530c1b68..79c1812e1f 100644 --- a/OsmAnd-telegram/src/main/java/net/osmand/telegram/LoginDialogFragment.kt +++ b/OsmAnd-telegram/src/main/java/net/osmand/telegram/LoginDialogFragment.kt @@ -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? = 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() } } diff --git a/OsmAnd-telegram/src/main/java/net/osmand/telegram/MainActivity.kt b/OsmAnd-telegram/src/main/java/net/osmand/telegram/MainActivity.kt index d0c7a7d183..0a0bd22066 100644 --- a/OsmAnd-telegram/src/main/java/net/osmand/telegram/MainActivity.kt +++ b/OsmAnd-telegram/src/main/java/net/osmand/telegram/MainActivity.kt @@ -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) diff --git a/OsmAnd-telegram/src/main/java/net/osmand/telegram/TelegramApplication.kt b/OsmAnd-telegram/src/main/java/net/osmand/telegram/TelegramApplication.kt new file mode 100644 index 0000000000..58f4252af2 --- /dev/null +++ b/OsmAnd-telegram/src/main/java/net/osmand/telegram/TelegramApplication.kt @@ -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 + } +} diff --git a/OsmAnd-telegram/src/main/java/net/osmand/telegram/TelegramHelper.kt b/OsmAnd-telegram/src/main/java/net/osmand/telegram/TelegramHelper.kt index 01cfa4869f..c7b574822e 100644 --- a/OsmAnd-telegram/src/main/java/net/osmand/telegram/TelegramHelper.kt +++ b/OsmAnd-telegram/src/main/java/net/osmand/telegram/TelegramHelper.kt @@ -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 -> { diff --git a/OsmAnd-telegram/src/main/res/layout/action_progress_bar.xml b/OsmAnd-telegram/src/main/res/layout/action_progress_bar.xml index b2f83088d2..1ac06e200d 100644 --- a/OsmAnd-telegram/src/main/res/layout/action_progress_bar.xml +++ b/OsmAnd-telegram/src/main/res/layout/action_progress_bar.xml @@ -2,7 +2,9 @@ + android:gravity="center" + android:paddingRight="16dp" + android:paddingLeft="16dp">