Added OsmAnd-telegram module
Drop telegram so files
30
OsmAnd-telegram/.gitignore
vendored
Normal file
|
@ -0,0 +1,30 @@
|
|||
gradle
|
||||
gradlew
|
||||
gradlew.bat
|
||||
|
||||
aarDependencies
|
||||
|
||||
bin/
|
||||
dist/
|
||||
gen/
|
||||
local.properties
|
||||
obj/
|
||||
out/
|
||||
use/
|
||||
|
||||
# Android Studio
|
||||
/.idea
|
||||
*.iml
|
||||
|
||||
# Gradle
|
||||
.gradle
|
||||
/local.properties
|
||||
|
||||
# MacOSX
|
||||
.DS_Store
|
||||
|
||||
# Output
|
||||
/build
|
||||
|
||||
# Project-specific
|
||||
*.so
|
85
OsmAnd-telegram/build.gradle
Normal file
|
@ -0,0 +1,85 @@
|
|||
apply plugin: 'com.android.application'
|
||||
|
||||
apply plugin: 'kotlin-android'
|
||||
apply plugin: 'kotlin-android-extensions'
|
||||
|
||||
android {
|
||||
compileSdkVersion 27
|
||||
|
||||
sourceSets.main {
|
||||
jniLibs.srcDir 'libs'
|
||||
jni.srcDirs = [] //disable automatic ndk-build call
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
applicationId "net.osmand.telegramtest"
|
||||
minSdkVersion 15
|
||||
targetSdkVersion 27
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
flavorDimensions "abi"
|
||||
|
||||
productFlavors {
|
||||
x86 {
|
||||
dimension "abi"
|
||||
ndk {
|
||||
abiFilter "x86"
|
||||
}
|
||||
}
|
||||
mips {
|
||||
dimension "abi"
|
||||
ndk {
|
||||
abiFilter "mips"
|
||||
}
|
||||
}
|
||||
armv7 {
|
||||
dimension "abi"
|
||||
ndk {
|
||||
abiFilter "armeabi-v7a"
|
||||
}
|
||||
}
|
||||
armv5 {
|
||||
dimension "abi"
|
||||
ndk {
|
||||
abiFilter "armeabi"
|
||||
}
|
||||
}
|
||||
fat {
|
||||
dimension "abi"
|
||||
}
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
debug {
|
||||
signingConfig android.signingConfigs.debug
|
||||
}
|
||||
nativeDebug {
|
||||
signingConfig android.signingConfigs.debug
|
||||
}
|
||||
release {
|
||||
signingConfig android.signingConfigs.debug
|
||||
}
|
||||
}
|
||||
|
||||
lintOptions {
|
||||
// use this line to check all rules except those listed
|
||||
disable 'InvalidPackage'
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
|
||||
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||||
implementation 'com.android.support:appcompat-v7:27.1.1'
|
||||
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
|
||||
implementation 'com.android.support:support-annotations:27.1.1'
|
||||
implementation 'commons-logging:commons-logging-api:1.1'
|
||||
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package net.osmand.telegramtest
|
||||
|
||||
import android.support.test.InstrumentationRegistry
|
||||
import android.support.test.runner.AndroidJUnit4
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Instrumented test, which will execute on an Android device.
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ExampleInstrumentedTest {
|
||||
@Test
|
||||
fun useAppContext() {
|
||||
// Context of the app under test.
|
||||
val appContext = InstrumentationRegistry.getTargetContext()
|
||||
assertEquals("net.osmand.telegramtest", appContext.packageName)
|
||||
}
|
||||
}
|
29
OsmAnd-telegram/src/main/AndroidManifest.xml
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="net.osmand.telegramtest">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:launchMode="singleTask"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:screenOrientation="unspecified"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme"
|
||||
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>
|
||||
|
||||
</manifest>
|
|
@ -0,0 +1,35 @@
|
|||
package net.osmand.telegramtest
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.res.Configuration
|
||||
import android.view.View
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
|
||||
object AndroidUtils {
|
||||
|
||||
private fun isHardwareKeyboardAvailable(context: Context): Boolean {
|
||||
return context.resources.configuration.keyboard != Configuration.KEYBOARD_NOKEYS
|
||||
}
|
||||
|
||||
fun softKeyboardDelayed(view: View) {
|
||||
view.post {
|
||||
if (!isHardwareKeyboardAvailable(view.context)) {
|
||||
val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
|
||||
imm?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun hideSoftKeyboard(activity: Activity, input: View?) {
|
||||
val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager?
|
||||
if (inputMethodManager != null) {
|
||||
if (input != null) {
|
||||
val windowToken = input.windowToken
|
||||
if (windowToken != null) {
|
||||
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,184 @@
|
|||
package net.osmand.telegramtest
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.DialogFragment
|
||||
import android.support.v4.app.FragmentManager
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.WindowManager
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.widget.Button
|
||||
import android.widget.EditText
|
||||
|
||||
|
||||
class LoginDialogFragment : DialogFragment() {
|
||||
|
||||
companion object {
|
||||
|
||||
private const val TAG = "LoginDialogFragment"
|
||||
private val LOG = PlatformUtil.getLog(LoginDialogFragment::class.java)
|
||||
|
||||
private const val ENTER_PHONE_NUMBER_PARAM_KEY: String = "enter_phone_number_param_key"
|
||||
private const val ENTER_CODE_PARAM_KEY = "enter_code_param_key"
|
||||
private const val ENTER_PASSWORD_PARAM_KEY = "enter_password_param_key"
|
||||
private const val SHOW_PROGRESS_PARAM_KEY = "show_progress_param_key"
|
||||
|
||||
fun showDialog(fragmentManager: FragmentManager, vararg loginDialogType: LoginDialogType) {
|
||||
try {
|
||||
var fragment = getFragment(fragmentManager)
|
||||
if (fragment == null) {
|
||||
fragment = LoginDialogFragment()
|
||||
val args = Bundle()
|
||||
for (t in loginDialogType) {
|
||||
args.putBoolean(t.paramKey, true)
|
||||
}
|
||||
fragment.arguments = args
|
||||
fragment.show(fragmentManager, TAG)
|
||||
} else {
|
||||
fragment.updateDialog(*loginDialogType)
|
||||
}
|
||||
} catch (e: RuntimeException) {
|
||||
LOG.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
fun dismiss(fragmentManager: FragmentManager) {
|
||||
getFragment(fragmentManager)?.dismiss()
|
||||
}
|
||||
|
||||
private fun getFragment(fragmentManager: FragmentManager): LoginDialogFragment? {
|
||||
return fragmentManager.findFragmentByTag(TAG) as LoginDialogFragment?
|
||||
}
|
||||
}
|
||||
|
||||
private var loginDialogActiveTypes: Set<LoginDialogType>? = null
|
||||
|
||||
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),
|
||||
ENTER_PASSWORD(ENTER_PASSWORD_PARAM_KEY, R.id.enterPasswordLayout, R.id.passwordEditText),
|
||||
SHOW_PROGRESS(SHOW_PROGRESS_PARAM_KEY, R.id.progressLayout, 0);
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.AppTheme_NoActionbar)
|
||||
val activity = requireActivity()
|
||||
val window = activity.window
|
||||
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
|
||||
}
|
||||
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
||||
|
||||
val loginDialogActiveTypes: MutableSet<LoginDialogType> = HashSet()
|
||||
|
||||
val args = savedInstanceState ?: arguments
|
||||
if (args != null) {
|
||||
for (t in LoginDialogType.values()) {
|
||||
if (args.getBoolean(t.paramKey, false)) {
|
||||
loginDialogActiveTypes.add(t)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.loginDialogActiveTypes = loginDialogActiveTypes
|
||||
|
||||
val view = inflater.inflate(R.layout.login_dialog, container)
|
||||
buildDialog(view)
|
||||
return view
|
||||
}
|
||||
|
||||
private fun buildDialog(view: View?) {
|
||||
val loginDialogActiveTypes = this.loginDialogActiveTypes
|
||||
var hasProgress = false
|
||||
var focusRequested = false
|
||||
for (t in LoginDialogType.values()) {
|
||||
val layout: View? = view?.findViewById(t.viewId)
|
||||
val contains = loginDialogActiveTypes?.contains(t) ?: false
|
||||
when {
|
||||
contains -> {
|
||||
if (t == LoginDialogType.SHOW_PROGRESS) {
|
||||
hasProgress = true
|
||||
}
|
||||
if (layout != null) {
|
||||
layout.visibility = View.VISIBLE
|
||||
val editText: EditText? = layout.findViewById(t.editorId)
|
||||
if (editText != null) {
|
||||
editText.setOnEditorActionListener { _, actionId, _ ->
|
||||
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
||||
applyAuthParam(t, editText.text.toString())
|
||||
return@setOnEditorActionListener true
|
||||
}
|
||||
false
|
||||
}
|
||||
if (!focusRequested) {
|
||||
editText.requestFocus()
|
||||
AndroidUtils.softKeyboardDelayed(editText)
|
||||
focusRequested = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> layout?.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
val continueButton: Button? = view?.findViewById(R.id.continueButton)
|
||||
if (continueButton != null) {
|
||||
continueButton.isEnabled = !hasProgress
|
||||
if (hasProgress) {
|
||||
continueButton.setOnClickListener(null)
|
||||
} else {
|
||||
continueButton.setOnClickListener {
|
||||
for (t in LoginDialogType.values()) {
|
||||
val layout: View? = view.findViewById(t.viewId)
|
||||
val contains = loginDialogActiveTypes?.contains(t) ?: false
|
||||
if (contains && layout != null) {
|
||||
val editText: EditText? = layout.findViewById(t.editorId)
|
||||
if (editText != null) {
|
||||
applyAuthParam(t, editText.text.toString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val cancelButton: Button? = view?.findViewById(R.id.calcelButton)
|
||||
cancelButton?.setOnClickListener {
|
||||
getMainActivity()?.close()
|
||||
}
|
||||
}
|
||||
|
||||
private fun applyAuthParam(t: LoginDialogType, value: String) {
|
||||
getMainActivity()?.applyAuthParam(this, t, value)
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
val loginDialogActiveTypes = this.loginDialogActiveTypes
|
||||
if (loginDialogActiveTypes != null) {
|
||||
for (t in loginDialogActiveTypes) {
|
||||
outState.putBoolean(t.paramKey, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getMainActivity(): MainActivity? {
|
||||
val activity = this.activity
|
||||
return if (activity != null) {
|
||||
activity as MainActivity
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun updateDialog(vararg loginDialogType: LoginDialogType) {
|
||||
val loginDialogActiveTypes: MutableSet<LoginDialogType> = HashSet()
|
||||
for (t in loginDialogType) {
|
||||
loginDialogActiveTypes.add(t)
|
||||
}
|
||||
this.loginDialogActiveTypes = loginDialogActiveTypes
|
||||
|
||||
buildDialog(view)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,177 @@
|
|||
package net.osmand.telegramtest
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.Fragment
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.widget.Toast
|
||||
import net.osmand.telegramtest.LoginDialogFragment.LoginDialogType
|
||||
import net.osmand.telegramtest.TelegramHelper.*
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity(), TelegramListener {
|
||||
|
||||
companion object {
|
||||
private const val LOGIN_MENU_ID = 0
|
||||
private const val LOGOUT_MENU_ID = 1
|
||||
private const val PROGRESS_MENU_ID = 2
|
||||
}
|
||||
|
||||
private val telegramHelper: TelegramHelper = TelegramHelper.instance
|
||||
private var authParamRequestHandler: AuthParamRequestHandler? = null
|
||||
private var paused: Boolean = false
|
||||
|
||||
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 {
|
||||
if (!paused) {
|
||||
showLoginDialog(paramType)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
telegramHelper.listener = this
|
||||
telegramHelper.init()
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
paused = false
|
||||
invalidateOptionsMenu()
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
paused = true
|
||||
}
|
||||
|
||||
override fun onTelegramStatusChanged(prevAutoState: AuthState, newAuthState: AuthState) {
|
||||
runOnUiThread {
|
||||
if (!paused) {
|
||||
val fm = supportFragmentManager
|
||||
when (newAuthState) {
|
||||
AuthState.READY,
|
||||
AuthState.CLOSED,
|
||||
AuthState.UNKNOWN -> LoginDialogFragment.dismiss(fm)
|
||||
else -> Unit
|
||||
}
|
||||
invalidateOptionsMenu()
|
||||
updateTitle()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun logout(silent: Boolean = false) {
|
||||
if (telegramHelper.getAuthState() == AuthState.READY) {
|
||||
telegramHelper.logout()
|
||||
} else {
|
||||
invalidateOptionsMenu()
|
||||
updateTitle()
|
||||
if (!silent) {
|
||||
Toast.makeText(this, R.string.not_logged_in, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun close() {
|
||||
telegramHelper.close()
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
|
||||
return when (item?.itemId) {
|
||||
LOGIN_MENU_ID -> {
|
||||
telegramHelper.init()
|
||||
true
|
||||
}
|
||||
LOGOUT_MENU_ID -> {
|
||||
logout()
|
||||
true
|
||||
}
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
|
||||
if (menu != null) {
|
||||
menu.clear()
|
||||
when (telegramHelper.getAuthState()) {
|
||||
AuthState.UNKNOWN,
|
||||
AuthState.WAIT_PARAMETERS,
|
||||
AuthState.WAIT_PHONE_NUMBER,
|
||||
AuthState.WAIT_CODE,
|
||||
AuthState.WAIT_PASSWORD,
|
||||
AuthState.LOGGING_OUT,
|
||||
AuthState.CLOSING -> createProgressMenuItem(menu)
|
||||
AuthState.READY -> createMenuItem(menu, LOGOUT_MENU_ID, R.string.shared_string_logout,
|
||||
MenuItem.SHOW_AS_ACTION_WITH_TEXT or MenuItem.SHOW_AS_ACTION_ALWAYS)
|
||||
AuthState.CLOSED -> createMenuItem(menu, LOGIN_MENU_ID, R.string.shared_string_login,
|
||||
MenuItem.SHOW_AS_ACTION_WITH_TEXT or MenuItem.SHOW_AS_ACTION_ALWAYS)
|
||||
}
|
||||
}
|
||||
return super.onCreateOptionsMenu(menu)
|
||||
}
|
||||
|
||||
private fun createMenuItem(m: Menu, id: Int, titleRes: Int, menuItemType: Int): MenuItem {
|
||||
val menuItem = m.add(0, id, 0, titleRes)
|
||||
menuItem.setOnMenuItemClickListener { item -> onOptionsItemSelected(item) }
|
||||
menuItem.setShowAsAction(menuItemType)
|
||||
return menuItem
|
||||
}
|
||||
|
||||
private fun createProgressMenuItem(m: Menu): MenuItem {
|
||||
|
||||
val menuItem = m.add(0, PROGRESS_MENU_ID, 0, "")
|
||||
menuItem.actionView = layoutInflater.inflate(R.layout.action_progress_bar, null)
|
||||
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
|
||||
return menuItem
|
||||
}
|
||||
|
||||
private fun updateTitle() {
|
||||
title = when (telegramHelper.getAuthState()) {
|
||||
|
||||
AuthState.UNKNOWN,
|
||||
AuthState.WAIT_PHONE_NUMBER,
|
||||
AuthState.WAIT_CODE,
|
||||
AuthState.WAIT_PASSWORD,
|
||||
AuthState.READY,
|
||||
AuthState.CLOSED -> getString(R.string.app_name)
|
||||
|
||||
AuthState.WAIT_PARAMETERS -> getString(R.string.initialization) + "..."
|
||||
AuthState.LOGGING_OUT -> getString(R.string.logging_out) + "..."
|
||||
AuthState.CLOSING -> getString(R.string.closing) + "..."
|
||||
}
|
||||
}
|
||||
|
||||
private fun showLoginDialog(authParamType: AuthParamType) {
|
||||
when (authParamType) {
|
||||
AuthParamType.PHONE_NUMBER -> LoginDialogFragment.showDialog(supportFragmentManager, LoginDialogType.ENTER_PHONE_NUMBER)
|
||||
AuthParamType.CODE -> LoginDialogFragment.showDialog(supportFragmentManager, LoginDialogType.ENTER_CODE)
|
||||
AuthParamType.PASSWORD -> LoginDialogFragment.showDialog(supportFragmentManager, LoginDialogType.ENTER_PASSWORD)
|
||||
}
|
||||
}
|
||||
|
||||
fun applyAuthParam(loginDialogFragment: LoginDialogFragment?, loginDialogType: LoginDialogType, text: String) {
|
||||
if (loginDialogFragment != null) {
|
||||
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)
|
||||
LoginDialogType.ENTER_PASSWORD -> authParamRequestHandler?.applyAuthParam(AuthParamType.PASSWORD, text)
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
telegramHelper.close()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,146 @@
|
|||
package net.osmand.telegramtest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
public class PlatformUtil {
|
||||
|
||||
public static String TAG = "net.osmand";
|
||||
private static class OsmandLogImplementation implements Log {
|
||||
|
||||
private final String fullName;
|
||||
private final String name;
|
||||
|
||||
public OsmandLogImplementation(String name){
|
||||
this.fullName = name;
|
||||
this.name = fullName.substring(fullName.lastIndexOf('.') + 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trace(Object message) {
|
||||
if(isTraceEnabled()){
|
||||
android.util.Log.d(TAG, name + " " + message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trace(Object message, Throwable t) {
|
||||
if(isTraceEnabled()){
|
||||
android.util.Log.d(TAG, name + " " + message, t);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(Object message) {
|
||||
if(isDebugEnabled()){
|
||||
android.util.Log.d(TAG, name + " " + message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(Object message, Throwable t) {
|
||||
if(isDebugEnabled()){
|
||||
android.util.Log.d(TAG, name + " " + message, t);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(Object message) {
|
||||
if(isErrorEnabled()){
|
||||
android.util.Log.e(TAG, name + " " + message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(Object message, Throwable t) {
|
||||
if(isErrorEnabled()){
|
||||
android.util.Log.e(TAG, name + " " + message, t);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fatal(Object message) {
|
||||
if(isFatalEnabled()){
|
||||
android.util.Log.e(TAG, name + " " + message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fatal(Object message, Throwable t) {
|
||||
if(isFatalEnabled()){
|
||||
android.util.Log.e(TAG, name + " " + message, t);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(Object message) {
|
||||
if(isInfoEnabled()){
|
||||
android.util.Log.i(TAG, name + " " + message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(Object message, Throwable t) {
|
||||
if(isInfoEnabled()){
|
||||
android.util.Log.i(TAG, name + " " + message, t);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTraceEnabled() {
|
||||
return android.util.Log.isLoggable(TAG, android.util.Log.VERBOSE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isDebugEnabled() {
|
||||
// For debug purposes always true
|
||||
// return android.util.Log.isLoggable(TAG, android.util.Log.DEBUG);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isErrorEnabled() {
|
||||
return android.util.Log.isLoggable(TAG, android.util.Log.ERROR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFatalEnabled() {
|
||||
return android.util.Log.isLoggable(TAG, android.util.Log.ERROR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return android.util.Log.isLoggable(TAG, android.util.Log.INFO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return android.util.Log.isLoggable(TAG, android.util.Log.WARN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(Object message) {
|
||||
if(isWarnEnabled()){
|
||||
android.util.Log.w(TAG, name + " " + message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(Object message, Throwable t) {
|
||||
if(isWarnEnabled()){
|
||||
android.util.Log.w(TAG, name + " " + message, t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Log getLog(String name){
|
||||
return new OsmandLogImplementation(name);
|
||||
}
|
||||
|
||||
public static Log getLog(Class<?> cl){
|
||||
return getLog(cl.getName());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,422 @@
|
|||
package net.osmand.telegramtest
|
||||
|
||||
import android.text.TextUtils
|
||||
import net.osmand.telegramtest.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.concurrent.ConcurrentHashMap
|
||||
|
||||
class TelegramHelper private constructor() {
|
||||
var appDir: String? = null
|
||||
private var libraryLoaded = false
|
||||
private var authParamRequestHandler: AuthParamRequestHandler? = null
|
||||
|
||||
private var client: Client? = null
|
||||
|
||||
private var authorizationState: AuthorizationState? = null
|
||||
private var isHaveAuthorization = false
|
||||
|
||||
private val defaultHandler = DefaultHandler()
|
||||
|
||||
var listener: TelegramListener? = null
|
||||
|
||||
enum class AuthParamType {
|
||||
PHONE_NUMBER,
|
||||
CODE,
|
||||
PASSWORD
|
||||
}
|
||||
|
||||
enum class AuthState {
|
||||
UNKNOWN,
|
||||
WAIT_PARAMETERS,
|
||||
WAIT_PHONE_NUMBER,
|
||||
WAIT_CODE,
|
||||
WAIT_PASSWORD,
|
||||
READY,
|
||||
LOGGING_OUT,
|
||||
CLOSING,
|
||||
CLOSED
|
||||
}
|
||||
|
||||
interface TelegramListener {
|
||||
fun onTelegramStatusChanged(prevAutoState: AuthState, newAuthState: AuthState)
|
||||
}
|
||||
|
||||
interface AuthParamRequestListener {
|
||||
fun onRequestAuthParam(paramType: AuthParamType)
|
||||
}
|
||||
|
||||
inner class AuthParamRequestHandler(val authParamRequestListener: AuthParamRequestListener) {
|
||||
|
||||
fun applyAuthParam(paramType: AuthParamType, paramValue: String) {
|
||||
if (!TextUtils.isEmpty(paramValue)) {
|
||||
when (paramType) {
|
||||
PHONE_NUMBER -> client!!.send(TdApi.SetAuthenticationPhoneNumber(paramValue, false, false), AuthorizationRequestHandler())
|
||||
CODE -> client!!.send(TdApi.CheckAuthenticationCode(paramValue, "", ""), AuthorizationRequestHandler())
|
||||
PASSWORD -> client!!.send(TdApi.CheckAuthenticationPassword(paramValue), AuthorizationRequestHandler())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getAuthState(): AuthState {
|
||||
val authorizationState = this.authorizationState ?: return AuthState.UNKNOWN
|
||||
return when (authorizationState.constructor) {
|
||||
TdApi.AuthorizationStateWaitTdlibParameters.CONSTRUCTOR -> AuthState.WAIT_PARAMETERS
|
||||
TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR -> AuthState.WAIT_PHONE_NUMBER
|
||||
TdApi.AuthorizationStateWaitCode.CONSTRUCTOR -> AuthState.WAIT_CODE
|
||||
TdApi.AuthorizationStateWaitPassword.CONSTRUCTOR -> AuthState.WAIT_PASSWORD
|
||||
TdApi.AuthorizationStateReady.CONSTRUCTOR -> AuthState.READY
|
||||
TdApi.AuthorizationStateLoggingOut.CONSTRUCTOR -> AuthState.LOGGING_OUT
|
||||
TdApi.AuthorizationStateClosing.CONSTRUCTOR -> AuthState.CLOSING
|
||||
TdApi.AuthorizationStateClosed.CONSTRUCTOR -> AuthState.CLOSED
|
||||
else -> AuthState.UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
fun setAuthParamRequestHandler(authParamRequestListener: AuthParamRequestListener): AuthParamRequestHandler {
|
||||
val authParamRequestHandler = AuthParamRequestHandler(authParamRequestListener)
|
||||
this.authParamRequestHandler = authParamRequestHandler
|
||||
return authParamRequestHandler
|
||||
}
|
||||
|
||||
init {
|
||||
try {
|
||||
log.debug("Loading native tdlib...")
|
||||
System.loadLibrary("tdjni")
|
||||
Client.setLogVerbosityLevel(0)
|
||||
libraryLoaded = true
|
||||
} catch (e: Throwable) {
|
||||
log.error("Failed to load tdlib", e)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun init(): Boolean {
|
||||
return if (libraryLoaded) {
|
||||
// create client
|
||||
client = Client.create(UpdatesHandler(), null, null)
|
||||
client!!.send(TdApi.GetAuthorizationState(), defaultHandler)
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fun logout(): Boolean {
|
||||
return if (libraryLoaded) {
|
||||
isHaveAuthorization = false
|
||||
client!!.send(TdApi.LogOut(), defaultHandler)
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fun close(): Boolean {
|
||||
return if (libraryLoaded) {
|
||||
isHaveAuthorization = false
|
||||
client!!.send(TdApi.Close(), defaultHandler)
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
private fun onAuthorizationStateUpdated(authorizationState: AuthorizationState?) {
|
||||
val prevAuthState = getAuthState()
|
||||
if (authorizationState != null) {
|
||||
this.authorizationState = authorizationState
|
||||
}
|
||||
when (this.authorizationState!!.constructor) {
|
||||
TdApi.AuthorizationStateWaitTdlibParameters.CONSTRUCTOR -> {
|
||||
log.info("Init tdlib parameters")
|
||||
|
||||
val parameters = TdApi.TdlibParameters()
|
||||
parameters.databaseDirectory = File(appDir, "tdlib").absolutePath
|
||||
parameters.useMessageDatabase = true
|
||||
parameters.useSecretChats = true
|
||||
parameters.apiId = 94575
|
||||
parameters.apiHash = "a3406de8d171bb422bb6ddf3bbd800e2"
|
||||
parameters.systemLanguageCode = "en"
|
||||
parameters.deviceModel = "Android"
|
||||
parameters.systemVersion = "Unknown"
|
||||
parameters.applicationVersion = "1.0"
|
||||
parameters.enableStorageOptimizer = true
|
||||
|
||||
client!!.send(TdApi.SetTdlibParameters(parameters), AuthorizationRequestHandler())
|
||||
}
|
||||
TdApi.AuthorizationStateWaitEncryptionKey.CONSTRUCTOR -> {
|
||||
client!!.send(TdApi.CheckDatabaseEncryptionKey(), AuthorizationRequestHandler())
|
||||
}
|
||||
TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR -> {
|
||||
log.info("Request phone number")
|
||||
authParamRequestHandler?.authParamRequestListener?.onRequestAuthParam(PHONE_NUMBER)
|
||||
}
|
||||
TdApi.AuthorizationStateWaitCode.CONSTRUCTOR -> {
|
||||
log.info("Request code")
|
||||
authParamRequestHandler?.authParamRequestListener?.onRequestAuthParam(CODE)
|
||||
}
|
||||
TdApi.AuthorizationStateWaitPassword.CONSTRUCTOR -> {
|
||||
log.info("Request password")
|
||||
authParamRequestHandler?.authParamRequestListener?.onRequestAuthParam(PASSWORD)
|
||||
}
|
||||
TdApi.AuthorizationStateReady.CONSTRUCTOR -> {
|
||||
isHaveAuthorization = true
|
||||
log.info("Ready")
|
||||
}
|
||||
TdApi.AuthorizationStateLoggingOut.CONSTRUCTOR -> {
|
||||
isHaveAuthorization = false
|
||||
log.info("Logging out")
|
||||
}
|
||||
TdApi.AuthorizationStateClosing.CONSTRUCTOR -> {
|
||||
isHaveAuthorization = false
|
||||
log.info("Closing")
|
||||
}
|
||||
TdApi.AuthorizationStateClosed.CONSTRUCTOR -> {
|
||||
log.info("Closed")
|
||||
}
|
||||
else -> log.error("Unsupported authorization state: " + this.authorizationState!!)
|
||||
}
|
||||
val newAuthState = getAuthState()
|
||||
listener?.onTelegramStatusChanged(prevAuthState, newAuthState)
|
||||
}
|
||||
|
||||
private class OrderedChat internal constructor(internal val order: Long, internal val chatId: Long) : Comparable<OrderedChat> {
|
||||
|
||||
override fun compareTo(other: OrderedChat): Int {
|
||||
if (this.order != other.order) {
|
||||
return if (other.order < this.order) -1 else 1
|
||||
}
|
||||
return if (this.chatId != other.chatId) {
|
||||
if (other.chatId < this.chatId) -1 else 1
|
||||
} else 0
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other == null) {
|
||||
return false
|
||||
}
|
||||
if (other !is OrderedChat) {
|
||||
return false
|
||||
}
|
||||
val o = other as OrderedChat?
|
||||
return this.order == o!!.order && this.chatId == o.chatId
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return (order + chatId).hashCode()
|
||||
}
|
||||
}
|
||||
|
||||
private class DefaultHandler : ResultHandler {
|
||||
override fun onResult(obj: TdApi.Object) {}
|
||||
}
|
||||
|
||||
private inner class UpdatesHandler : ResultHandler {
|
||||
override fun onResult(obj: TdApi.Object) {
|
||||
when (obj.constructor) {
|
||||
TdApi.UpdateAuthorizationState.CONSTRUCTOR -> onAuthorizationStateUpdated((obj as TdApi.UpdateAuthorizationState).authorizationState)
|
||||
|
||||
TdApi.UpdateUser.CONSTRUCTOR -> {
|
||||
val updateUser = obj as TdApi.UpdateUser
|
||||
users[updateUser.user.id] = updateUser.user
|
||||
}
|
||||
TdApi.UpdateUserStatus.CONSTRUCTOR -> {
|
||||
val updateUserStatus = obj as TdApi.UpdateUserStatus
|
||||
val user = users[updateUserStatus.userId]
|
||||
synchronized(user!!) {
|
||||
user.status = updateUserStatus.status
|
||||
}
|
||||
}
|
||||
TdApi.UpdateBasicGroup.CONSTRUCTOR -> {
|
||||
val updateBasicGroup = obj as TdApi.UpdateBasicGroup
|
||||
basicGroups[updateBasicGroup.basicGroup.id] = updateBasicGroup.basicGroup
|
||||
}
|
||||
TdApi.UpdateSupergroup.CONSTRUCTOR -> {
|
||||
val updateSupergroup = obj as TdApi.UpdateSupergroup
|
||||
supergroups[updateSupergroup.supergroup.id] = updateSupergroup.supergroup
|
||||
}
|
||||
TdApi.UpdateSecretChat.CONSTRUCTOR -> {
|
||||
val updateSecretChat = obj as TdApi.UpdateSecretChat
|
||||
secretChats[updateSecretChat.secretChat.id] = updateSecretChat.secretChat
|
||||
}
|
||||
|
||||
TdApi.UpdateNewChat.CONSTRUCTOR -> {
|
||||
val updateNewChat = obj as TdApi.UpdateNewChat
|
||||
val chat = updateNewChat.chat
|
||||
synchronized(chat!!) {
|
||||
chats[chat.id] = chat
|
||||
|
||||
val order = chat.order
|
||||
chat.order = 0
|
||||
setChatOrder(chat, order)
|
||||
}
|
||||
}
|
||||
TdApi.UpdateChatTitle.CONSTRUCTOR -> {
|
||||
val updateChat = obj as TdApi.UpdateChatTitle
|
||||
val chat = chats[updateChat.chatId]
|
||||
synchronized(chat!!) {
|
||||
chat.title = updateChat.title
|
||||
}
|
||||
}
|
||||
TdApi.UpdateChatPhoto.CONSTRUCTOR -> {
|
||||
val updateChat = obj as TdApi.UpdateChatPhoto
|
||||
val chat = chats[updateChat.chatId]
|
||||
synchronized(chat!!) {
|
||||
chat.photo = updateChat.photo
|
||||
}
|
||||
}
|
||||
TdApi.UpdateChatLastMessage.CONSTRUCTOR -> {
|
||||
val updateChat = obj as TdApi.UpdateChatLastMessage
|
||||
val chat = chats[updateChat.chatId]
|
||||
synchronized(chat!!) {
|
||||
chat.lastMessage = updateChat.lastMessage
|
||||
setChatOrder(chat, updateChat.order)
|
||||
}
|
||||
}
|
||||
TdApi.UpdateChatOrder.CONSTRUCTOR -> {
|
||||
val updateChat = obj as TdApi.UpdateChatOrder
|
||||
val chat = chats[updateChat.chatId]
|
||||
synchronized(chat!!) {
|
||||
setChatOrder(chat, updateChat.order)
|
||||
}
|
||||
}
|
||||
TdApi.UpdateChatIsPinned.CONSTRUCTOR -> {
|
||||
val updateChat = obj as TdApi.UpdateChatIsPinned
|
||||
val chat = chats[updateChat.chatId]
|
||||
synchronized(chat!!) {
|
||||
chat.isPinned = updateChat.isPinned
|
||||
setChatOrder(chat, updateChat.order)
|
||||
}
|
||||
}
|
||||
TdApi.UpdateChatReadInbox.CONSTRUCTOR -> {
|
||||
val updateChat = obj as TdApi.UpdateChatReadInbox
|
||||
val chat = chats[updateChat.chatId]
|
||||
synchronized(chat!!) {
|
||||
chat.lastReadInboxMessageId = updateChat.lastReadInboxMessageId
|
||||
chat.unreadCount = updateChat.unreadCount
|
||||
}
|
||||
}
|
||||
TdApi.UpdateChatReadOutbox.CONSTRUCTOR -> {
|
||||
val updateChat = obj as TdApi.UpdateChatReadOutbox
|
||||
val chat = chats[updateChat.chatId]
|
||||
synchronized(chat!!) {
|
||||
chat.lastReadOutboxMessageId = updateChat.lastReadOutboxMessageId
|
||||
}
|
||||
}
|
||||
TdApi.UpdateChatUnreadMentionCount.CONSTRUCTOR -> {
|
||||
val updateChat = obj as TdApi.UpdateChatUnreadMentionCount
|
||||
val chat = chats[updateChat.chatId]
|
||||
synchronized(chat!!) {
|
||||
chat.unreadMentionCount = updateChat.unreadMentionCount
|
||||
}
|
||||
}
|
||||
TdApi.UpdateMessageMentionRead.CONSTRUCTOR -> {
|
||||
val updateChat = obj as TdApi.UpdateMessageMentionRead
|
||||
val chat = chats[updateChat.chatId]
|
||||
synchronized(chat!!) {
|
||||
chat.unreadMentionCount = updateChat.unreadMentionCount
|
||||
}
|
||||
}
|
||||
TdApi.UpdateChatReplyMarkup.CONSTRUCTOR -> {
|
||||
val updateChat = obj as TdApi.UpdateChatReplyMarkup
|
||||
val chat = chats[updateChat.chatId]
|
||||
synchronized(chat!!) {
|
||||
chat.replyMarkupMessageId = updateChat.replyMarkupMessageId
|
||||
}
|
||||
}
|
||||
TdApi.UpdateChatDraftMessage.CONSTRUCTOR -> {
|
||||
val updateChat = obj as TdApi.UpdateChatDraftMessage
|
||||
val chat = chats[updateChat.chatId]
|
||||
synchronized(chat!!) {
|
||||
chat.draftMessage = updateChat.draftMessage
|
||||
setChatOrder(chat, updateChat.order)
|
||||
}
|
||||
}
|
||||
TdApi.UpdateNotificationSettings.CONSTRUCTOR -> {
|
||||
val update = obj as TdApi.UpdateNotificationSettings
|
||||
if (update.scope is TdApi.NotificationSettingsScopeChat) {
|
||||
val chat = chats[(update.scope as TdApi.NotificationSettingsScopeChat).chatId]
|
||||
synchronized(chat!!) {
|
||||
chat.notificationSettings = update.notificationSettings
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TdApi.UpdateUserFullInfo.CONSTRUCTOR -> {
|
||||
val updateUserFullInfo = obj as TdApi.UpdateUserFullInfo
|
||||
usersFullInfo[updateUserFullInfo.userId] = updateUserFullInfo.userFullInfo
|
||||
}
|
||||
TdApi.UpdateBasicGroupFullInfo.CONSTRUCTOR -> {
|
||||
val updateBasicGroupFullInfo = obj as TdApi.UpdateBasicGroupFullInfo
|
||||
basicGroupsFullInfo[updateBasicGroupFullInfo.basicGroupId] = updateBasicGroupFullInfo.basicGroupFullInfo
|
||||
}
|
||||
TdApi.UpdateSupergroupFullInfo.CONSTRUCTOR -> {
|
||||
val updateSupergroupFullInfo = obj as TdApi.UpdateSupergroupFullInfo
|
||||
supergroupsFullInfo[updateSupergroupFullInfo.supergroupId] = updateSupergroupFullInfo.supergroupFullInfo
|
||||
}
|
||||
}// print("Unsupported update:" + newLine + object);
|
||||
}
|
||||
}
|
||||
|
||||
private inner class AuthorizationRequestHandler : ResultHandler {
|
||||
override fun onResult(obj: TdApi.Object) {
|
||||
when (obj.constructor) {
|
||||
TdApi.Error.CONSTRUCTOR -> {
|
||||
log.error("Receive an error: $obj")
|
||||
onAuthorizationStateUpdated(null) // repeat last action
|
||||
}
|
||||
TdApi.Ok.CONSTRUCTOR -> {
|
||||
}
|
||||
else -> log.error("Receive wrong response from TDLib: $obj")
|
||||
}// result is already received through UpdateAuthorizationState, nothing to do
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val log = PlatformUtil.getLog(TelegramHelper::class.java)
|
||||
|
||||
private var helper: TelegramHelper? = null
|
||||
|
||||
private val users = ConcurrentHashMap<Int, TdApi.User>()
|
||||
private val basicGroups = ConcurrentHashMap<Int, TdApi.BasicGroup>()
|
||||
private val supergroups = ConcurrentHashMap<Int, TdApi.Supergroup>()
|
||||
private val secretChats = ConcurrentHashMap<Int, TdApi.SecretChat>()
|
||||
|
||||
private val chats = ConcurrentHashMap<Long, TdApi.Chat>()
|
||||
private val chatList = TreeSet<OrderedChat>()
|
||||
|
||||
private val usersFullInfo = ConcurrentHashMap<Int, TdApi.UserFullInfo>()
|
||||
private val basicGroupsFullInfo = ConcurrentHashMap<Int, TdApi.BasicGroupFullInfo>()
|
||||
private val supergroupsFullInfo = ConcurrentHashMap<Int, TdApi.SupergroupFullInfo>()
|
||||
|
||||
val instance: TelegramHelper
|
||||
get() {
|
||||
if (helper == null) {
|
||||
helper = TelegramHelper()
|
||||
}
|
||||
return helper!!
|
||||
}
|
||||
|
||||
private fun setChatOrder(chat: TdApi.Chat, order: Long) {
|
||||
synchronized(chatList) {
|
||||
if (chat.order != 0L) {
|
||||
chatList.remove(OrderedChat(chat.order, chat.id))
|
||||
}
|
||||
|
||||
chat.order = order
|
||||
|
||||
if (chat.order != 0L) {
|
||||
chatList.add(OrderedChat(chat.order, chat.id))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
389
OsmAnd-telegram/src/main/java/org/drinkless/td/libcore/telegram/Client.java
Executable file
|
@ -0,0 +1,389 @@
|
|||
/*
|
||||
* This file is part of TD.
|
||||
*
|
||||
* TD is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* TD is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with TD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2014-2015 Arseny Smirnov
|
||||
* 2014-2015 Aliaksei Levin
|
||||
*/
|
||||
|
||||
package org.drinkless.td.libcore.telegram;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
/**
|
||||
* Main class for interaction with the TDLib.
|
||||
*/
|
||||
public class Client implements Runnable {
|
||||
/**
|
||||
* Interface for handler for results of queries to TDLib and incoming updates from TDLib.
|
||||
*/
|
||||
public interface ResultHandler {
|
||||
/**
|
||||
* Callback called on result of query to TDLib or incoming update from TDLib.
|
||||
*
|
||||
* @param object Result of query or update of type TdApi.Update about new events.
|
||||
*/
|
||||
void onResult(TdApi.Object object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface for handler of exceptions thrown while invoking ResultHandler.
|
||||
* By default, all such exceptions are ignored.
|
||||
* All exceptions thrown from ExceptionHandler are ignored.
|
||||
*/
|
||||
public interface ExceptionHandler {
|
||||
/**
|
||||
* Callback called on exceptions thrown while invoking ResultHandler.
|
||||
*
|
||||
* @param e Exception thrown by ResultHandler.
|
||||
*/
|
||||
void onException(Throwable e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a request to the TDLib.
|
||||
*
|
||||
* @param query Object representing a query to the TDLib.
|
||||
* @param resultHandler Result handler with onResult method which will be called with result
|
||||
* of the query or with TdApi.Error as parameter. If it is null, nothing
|
||||
* will be called.
|
||||
* @param exceptionHandler Exception handler with onException method which will be called on
|
||||
* exception thrown from resultHandler. If it is null, then
|
||||
* defaultExceptionHandler will be called.
|
||||
* @throws NullPointerException if query is null.
|
||||
*/
|
||||
public void send(TdApi.Function query, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
|
||||
if (query == null) {
|
||||
throw new NullPointerException("query is null");
|
||||
}
|
||||
|
||||
readLock.lock();
|
||||
try {
|
||||
if (isClientDestroyed) {
|
||||
if (resultHandler != null) {
|
||||
handleResult(new TdApi.Error(500, "Client is closed"), resultHandler, exceptionHandler);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
long queryId = currentQueryId.incrementAndGet();
|
||||
handlers.put(queryId, new Handler(resultHandler, exceptionHandler));
|
||||
NativeClient.clientSend(nativeClientId, queryId, query);
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a request to the TDLib with an empty ExceptionHandler.
|
||||
*
|
||||
* @param query Object representing a query to the TDLib.
|
||||
* @param resultHandler Result handler with onResult method which will be called with result
|
||||
* of the query or with TdApi.Error as parameter. If it is null, then
|
||||
* defaultExceptionHandler will be called.
|
||||
* @throws NullPointerException if query is null.
|
||||
*/
|
||||
public void send(TdApi.Function query, ResultHandler resultHandler) {
|
||||
send(query, resultHandler, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronously executes a TDLib request. Only a few marked accordingly requests can be executed synchronously.
|
||||
*
|
||||
* @param query Object representing a query to the TDLib.
|
||||
* @throws NullPointerException if query is null.
|
||||
* @return request result.
|
||||
*/
|
||||
public static TdApi.Object execute(TdApi.Function query) {
|
||||
if (query == null) {
|
||||
throw new NullPointerException("query is null");
|
||||
}
|
||||
return NativeClient.clientExecute(query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces handler for incoming updates from the TDLib.
|
||||
*
|
||||
* @param updatesHandler Handler with onResult method which will be called for every incoming
|
||||
* update from the TDLib.
|
||||
* @param exceptionHandler Exception handler with onException method which will be called on
|
||||
* exception thrown from updatesHandler, if it is null, defaultExceptionHandler will be invoked.
|
||||
*/
|
||||
public void setUpdatesHandler(ResultHandler updatesHandler, ExceptionHandler exceptionHandler) {
|
||||
handlers.put(0L, new Handler(updatesHandler, exceptionHandler));
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces handler for incoming updates from the TDLib. Sets empty ExceptionHandler.
|
||||
*
|
||||
* @param updatesHandler Handler with onResult method which will be called for every incoming
|
||||
* update from the TDLib.
|
||||
*/
|
||||
public void setUpdatesHandler(ResultHandler updatesHandler) {
|
||||
setUpdatesHandler(updatesHandler, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces default exception handler to be invoked on exceptions thrown from updatesHandler and all other ResultHandler.
|
||||
*
|
||||
* @param defaultExceptionHandler Default exception handler. If null Exceptions are ignored.
|
||||
*/
|
||||
public void setDefaultExceptionHandler(Client.ExceptionHandler defaultExceptionHandler) {
|
||||
this.defaultExceptionHandler = defaultExceptionHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for benchmarking number of queries per second which can handle the TDLib, ignore it.
|
||||
*
|
||||
* @param query Object representing a query to the TDLib.
|
||||
* @param handler Result handler with onResult method which will be called with result
|
||||
* of the query or with TdApi.Error as parameter.
|
||||
* @param count Number of times to repeat the query.
|
||||
* @throws NullPointerException if query is null.
|
||||
*/
|
||||
public void bench(TdApi.Function query, ResultHandler handler, int count) {
|
||||
if (query == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
send(query, handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden method from Runnable, do not call it directly.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stopFlag) {
|
||||
receiveQueries(300.0 /*seconds*/);
|
||||
}
|
||||
Log.d("DLTD", "Stop TDLib thread");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new Client.
|
||||
*
|
||||
* @param updatesHandler Handler for incoming updates.
|
||||
* @param updatesExceptionHandler Handler for exceptions thrown from updatesHandler. If it is null, exceptions will be iggnored.
|
||||
* @param defaultExceptionHandler Default handler for exceptions thrown from all ResultHandler. If it is null, exceptions will be iggnored.
|
||||
* @return created Client
|
||||
*/
|
||||
public static Client create(ResultHandler updatesHandler, ExceptionHandler updatesExceptionHandler, ExceptionHandler defaultExceptionHandler) {
|
||||
Client client = new Client(updatesHandler, updatesExceptionHandler, defaultExceptionHandler);
|
||||
new Thread(client, "TDLib thread").start();
|
||||
return client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes TDLib log verbosity.
|
||||
*
|
||||
* @param newLogVerbosity New value of log verbosity. Must be non-negative.
|
||||
* Value 0 corresponds to android.util.Log.ASSERT,
|
||||
* value 1 corresponds to android.util.Log.ERROR,
|
||||
* value 2 corresponds to android.util.Log.WARNING,
|
||||
* value 3 corresponds to android.util.Log.INFO,
|
||||
* value 4 corresponds to android.util.Log.DEBUG,
|
||||
* value 5 corresponds to android.util.Log.VERBOSE,
|
||||
* value greater than 5 can be used to enable even more logging.
|
||||
* Default value of the log verbosity is 5.
|
||||
* @throws IllegalArgumentException if newLogVerbosity is negative.
|
||||
*/
|
||||
public static void setLogVerbosityLevel(int newLogVerbosity) {
|
||||
if (newLogVerbosity < 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
NativeClient.setLogVerbosityLevel(newLogVerbosity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets file path for writing TDLib internal log.
|
||||
* By default TDLib writes logs to the Android Log.
|
||||
* Use this method to write the log to a file instead.
|
||||
*
|
||||
* @param filePath Path to a file for writing TDLib internal log. Use an empty path to
|
||||
* switch back to logging to the Android Log.
|
||||
* @return whether opening the log file succeeded
|
||||
*/
|
||||
public static boolean setLogFilePath(String filePath) {
|
||||
return NativeClient.setLogFilePath(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes maximum size of TDLib log file.
|
||||
*
|
||||
* @param maxFileSize Maximum size of the file to where the internal TDLib log is written
|
||||
* before the file will be auto-rotated. Must be positive. Defaults to 10 MB.
|
||||
* @throws IllegalArgumentException if max_file_size is non-positive.
|
||||
*/
|
||||
public static void setLogMaxFileSize(long maxFileSize) {
|
||||
if (maxFileSize <= 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
NativeClient.setLogMaxFileSize(maxFileSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes Client.
|
||||
*/
|
||||
public void close() {
|
||||
writeLock.lock();
|
||||
try {
|
||||
if (isClientDestroyed) {
|
||||
return;
|
||||
}
|
||||
if (!stopFlag) {
|
||||
send(new TdApi.Close(), null);
|
||||
}
|
||||
isClientDestroyed = true;
|
||||
while (!stopFlag) {
|
||||
Thread.yield();
|
||||
}
|
||||
while (handlers.size() != 1) {
|
||||
receiveQueries(300.0);
|
||||
}
|
||||
NativeClient.destroyClient(nativeClientId);
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called from the JNI when a fatal error happens to provide a better error message.
|
||||
* It shouldn't return. Do not call it directly.
|
||||
*
|
||||
* @param errorMessage Error message.
|
||||
*/
|
||||
static void onFatalError(String errorMessage) {
|
||||
class ThrowError implements Runnable {
|
||||
private ThrowError(String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
throw new RuntimeException("TDLib fatal error: " + errorMessage);
|
||||
}
|
||||
|
||||
private final String errorMessage;
|
||||
}
|
||||
new Thread(new ThrowError(errorMessage), "TDLib fatal error thread").start();
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(1000); // 1000 milliseconds is one second.
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
|
||||
private final Lock readLock = readWriteLock.readLock();
|
||||
private final Lock writeLock = readWriteLock.writeLock();
|
||||
|
||||
private volatile boolean stopFlag = false;
|
||||
private volatile boolean isClientDestroyed = false;
|
||||
private final long nativeClientId;
|
||||
|
||||
private final ConcurrentHashMap<Long, Handler> handlers = new ConcurrentHashMap<Long, Handler>();
|
||||
private final AtomicLong currentQueryId = new AtomicLong();
|
||||
|
||||
private volatile ExceptionHandler defaultExceptionHandler = null;
|
||||
|
||||
private static final int MAX_EVENTS = 1000;
|
||||
private final long[] eventIds = new long[MAX_EVENTS];
|
||||
private final TdApi.Object[] events = new TdApi.Object[MAX_EVENTS];
|
||||
|
||||
private static class Handler {
|
||||
final ResultHandler resultHandler;
|
||||
final ExceptionHandler exceptionHandler;
|
||||
|
||||
Handler(ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
|
||||
this.resultHandler = resultHandler;
|
||||
this.exceptionHandler = exceptionHandler;
|
||||
}
|
||||
}
|
||||
|
||||
private Client(ResultHandler updatesHandler, ExceptionHandler updateExceptionHandler, ExceptionHandler defaultExceptionHandler) {
|
||||
nativeClientId = NativeClient.createClient();
|
||||
handlers.put(0L, new Handler(updatesHandler, updateExceptionHandler));
|
||||
this.defaultExceptionHandler = defaultExceptionHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
try {
|
||||
close();
|
||||
} finally {
|
||||
super.finalize();
|
||||
}
|
||||
}
|
||||
|
||||
private void processResult(long id, TdApi.Object object) {
|
||||
Handler handler;
|
||||
if (object instanceof TdApi.UpdateAuthorizationState) {
|
||||
if (((TdApi.UpdateAuthorizationState) object).authorizationState instanceof TdApi.AuthorizationStateClosed) {
|
||||
stopFlag = true;
|
||||
}
|
||||
}
|
||||
if (id == 0) {
|
||||
// update handler stays forever
|
||||
handler = handlers.get(id);
|
||||
} else {
|
||||
handler = handlers.remove(id);
|
||||
}
|
||||
if (handler == null) {
|
||||
Log.e("DLTD", "Can't find handler for the result " + id + " -- ignore result");
|
||||
return;
|
||||
}
|
||||
|
||||
handleResult(object, handler.resultHandler, handler.exceptionHandler);
|
||||
}
|
||||
|
||||
private void handleResult(TdApi.Object object, ResultHandler resultHandler, ExceptionHandler exceptionHandler) {
|
||||
if (resultHandler == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
resultHandler.onResult(object);
|
||||
} catch (Throwable cause) {
|
||||
if (exceptionHandler == null) {
|
||||
exceptionHandler = defaultExceptionHandler;
|
||||
}
|
||||
if (exceptionHandler != null) {
|
||||
try {
|
||||
exceptionHandler.onException(cause);
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void receiveQueries(double timeout) {
|
||||
int resultN = NativeClient.clientReceive(nativeClientId, eventIds, events, timeout);
|
||||
for (int i = 0; i < resultN; i++) {
|
||||
processResult(eventIds[i], events[i]);
|
||||
events[i] = null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* This file is part of TD.
|
||||
*
|
||||
* TD is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* TD is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with TD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Copyright 2014-2015 Arseny Smirnov
|
||||
* 2014-2015 Aliaksei Levin
|
||||
*/
|
||||
|
||||
package org.drinkless.td.libcore.telegram;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* This class is used internally by Client to send requests to the TDLib.
|
||||
*/
|
||||
final class NativeClient {
|
||||
static {
|
||||
try {
|
||||
System.loadLibrary("tdjni");
|
||||
Log.w("DLTD", "TDJNI loaded");
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
Log.w("DLTD", "Can't find tdjni", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static native long createClient();
|
||||
|
||||
public static native void destroyClient(long clientId);
|
||||
|
||||
public static native void clientSend(long clientId, long eventId, TdApi.Object event);
|
||||
|
||||
public static native int clientReceive(long clientId, long[] eventIds, TdApi.Object[] events, double timeout);
|
||||
|
||||
public static native TdApi.Object clientExecute(TdApi.Object event);
|
||||
|
||||
public static native void setLogVerbosityLevel(int newLogVerbosity);
|
||||
|
||||
public static native boolean setLogFilePath(String filePath);
|
||||
|
||||
public static native void setLogMaxFileSize(long maxFileSize);
|
||||
|
||||
//Just for testing
|
||||
public static native TdApi.Object pingPong(TdApi.Object object);
|
||||
|
||||
public static native void ping(TdApi.Object object);
|
||||
}
|
33230
OsmAnd-telegram/src/main/java/org/drinkless/td/libcore/telegram/TdApi.java
Executable file
|
@ -0,0 +1,34 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:aapt="http://schemas.android.com/aapt"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportHeight="108"
|
||||
android:viewportWidth="108">
|
||||
<path
|
||||
android:fillType="evenOdd"
|
||||
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
|
||||
android:strokeColor="#00000000"
|
||||
android:strokeWidth="1">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient
|
||||
android:endX="78.5885"
|
||||
android:endY="90.9159"
|
||||
android:startX="48.7653"
|
||||
android:startY="61.0927"
|
||||
android:type="linear">
|
||||
<item
|
||||
android:color="#44000000"
|
||||
android:offset="0.0" />
|
||||
<item
|
||||
android:color="#00000000"
|
||||
android:offset="1.0" />
|
||||
</gradient>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<path
|
||||
android:fillColor="#FFFFFF"
|
||||
android:fillType="nonZero"
|
||||
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
|
||||
android:strokeColor="#00000000"
|
||||
android:strokeWidth="1" />
|
||||
</vector>
|
170
OsmAnd-telegram/src/main/res/drawable/ic_launcher_background.xml
Normal file
|
@ -0,0 +1,170 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="108dp"
|
||||
android:height="108dp"
|
||||
android:viewportHeight="108"
|
||||
android:viewportWidth="108">
|
||||
<path
|
||||
android:fillColor="#26A69A"
|
||||
android:pathData="M0,0h108v108h-108z" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M9,0L9,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,0L19,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,0L29,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,0L39,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,0L49,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,0L59,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,0L69,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,0L79,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M89,0L89,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M99,0L99,108"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,9L108,9"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,19L108,19"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,29L108,29"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,39L108,39"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,49L108,49"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,59L108,59"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,69L108,69"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,79L108,79"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,89L108,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M0,99L108,99"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,29L89,29"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,39L89,39"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,49L89,49"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,59L89,59"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,69L89,69"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M19,79L89,79"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M29,19L29,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M39,19L39,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M49,19L49,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M59,19L59,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M69,19L69,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
<path
|
||||
android:fillColor="#00000000"
|
||||
android:pathData="M79,19L79,89"
|
||||
android:strokeColor="#33FFFFFF"
|
||||
android:strokeWidth="0.8" />
|
||||
</vector>
|
11
OsmAnd-telegram/src/main/res/layout/action_progress_bar.xml
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center">
|
||||
|
||||
<ProgressBar
|
||||
android:layout_width="32dp"
|
||||
android:layout_height="32dp" />
|
||||
|
||||
</LinearLayout>
|
16
OsmAnd-telegram/src/main/res/layout/activity_main.xml
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:gravity="center"
|
||||
android:text="Hello World!" />
|
||||
|
||||
</LinearLayout>
|
143
OsmAnd-telegram/src/main/res/layout/login_dialog.xml
Normal file
|
@ -0,0 +1,143 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:fillViewport="true"
|
||||
android:paddingBottom="24dp"
|
||||
android:paddingLeft="48dp"
|
||||
android:paddingRight="48dp"
|
||||
android:paddingTop="24dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="0.2"
|
||||
android:gravity="center"
|
||||
android:text="@string/login_to_telegram"
|
||||
android:textSize="22sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_gravity="center"
|
||||
android:layout_weight="0.6"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/enterPhoneNumberLayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:visibility="visible">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/phone_number_title" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/phoneNumberEditText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/phone_number_title"
|
||||
android:inputType="phone"
|
||||
tools:text="+380661234567" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/enterCodeLayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:visibility="gone">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/authentication_code" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/codeEditText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/authentication_code"
|
||||
android:inputType="number"
|
||||
tools:text="123456" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/enterPasswordLayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:visibility="gone">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/shared_string_password" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/passwordEditText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/shared_string_password"
|
||||
android:inputType="textPassword"
|
||||
tools:text="12345678" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/progressLayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:orientation="vertical"
|
||||
android:visibility="visible">
|
||||
|
||||
<ProgressBar
|
||||
android:layout_width="32dp"
|
||||
android:layout_height="32dp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="0.2"
|
||||
android:gravity="bottom"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<Button
|
||||
android:id="@+id/calcelButton"
|
||||
style="?android:attr/buttonBarButtonStyle"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/shared_string_cancel" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/continueButton"
|
||||
style="?android:attr/buttonBarButtonStyle"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/shared_string_continue" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<background android:drawable="@drawable/ic_launcher_background" />
|
||||
<foreground android:drawable="@drawable/ic_launcher_foreground" />
|
||||
</adaptive-icon>
|
BIN
OsmAnd-telegram/src/main/res/mipmap-hdpi/ic_launcher.png
Normal file
After Width: | Height: | Size: 3 KiB |
BIN
OsmAnd-telegram/src/main/res/mipmap-hdpi/ic_launcher_round.png
Normal file
After Width: | Height: | Size: 4.9 KiB |
BIN
OsmAnd-telegram/src/main/res/mipmap-mdpi/ic_launcher.png
Normal file
After Width: | Height: | Size: 2 KiB |
BIN
OsmAnd-telegram/src/main/res/mipmap-mdpi/ic_launcher_round.png
Normal file
After Width: | Height: | Size: 2.8 KiB |
BIN
OsmAnd-telegram/src/main/res/mipmap-xhdpi/ic_launcher.png
Normal file
After Width: | Height: | Size: 4.5 KiB |
BIN
OsmAnd-telegram/src/main/res/mipmap-xhdpi/ic_launcher_round.png
Normal file
After Width: | Height: | Size: 6.9 KiB |
BIN
OsmAnd-telegram/src/main/res/mipmap-xxhdpi/ic_launcher.png
Normal file
After Width: | Height: | Size: 6.3 KiB |
BIN
OsmAnd-telegram/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
OsmAnd-telegram/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Normal file
After Width: | Height: | Size: 9 KiB |
After Width: | Height: | Size: 15 KiB |
6
OsmAnd-telegram/src/main/res/values/colors.xml
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<color name="colorPrimary">#3F51B5</color>
|
||||
<color name="colorPrimaryDark">#303F9F</color>
|
||||
<color name="colorAccent">#FF4081</color>
|
||||
</resources>
|
15
OsmAnd-telegram/src/main/res/values/strings.xml
Normal file
|
@ -0,0 +1,15 @@
|
|||
<resources>
|
||||
<string name="app_name">Telegram Test</string>
|
||||
<string name="phone_number_title">Phone number in international format</string>
|
||||
<string name="shared_string_password">Password</string>
|
||||
<string name="authentication_code">Authentication code</string>
|
||||
<string name="login_to_telegram">Login to telegram</string>
|
||||
<string name="shared_string_login">Login</string>
|
||||
<string name="shared_string_logout">Logout</string>
|
||||
<string name="not_logged_in">You are not logged in</string>
|
||||
<string name="initialization">Initialization</string>
|
||||
<string name="logging_out">Logging out</string>
|
||||
<string name="closing">Closing</string>
|
||||
<string name="shared_string_continue">Continue</string>
|
||||
<string name="shared_string_cancel">Cancel</string>
|
||||
</resources>
|
16
OsmAnd-telegram/src/main/res/values/styles.xml
Normal file
|
@ -0,0 +1,16 @@
|
|||
<resources>
|
||||
|
||||
<!-- Base application theme. -->
|
||||
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="colorPrimary">@color/colorPrimary</item>
|
||||
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
|
||||
<item name="colorAccent">@color/colorAccent</item>
|
||||
</style>
|
||||
|
||||
<style name="AppTheme.NoActionbar">
|
||||
<item name="windowActionBar">false</item>
|
||||
<item name="windowNoTitle">true</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
|
@ -0,0 +1,17 @@
|
|||
package net.osmand.telegramtest
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
class ExampleUnitTest {
|
||||
@Test
|
||||
fun addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2)
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
buildscript {
|
||||
ext.kotlin_version = '1.2.41'
|
||||
repositories {
|
||||
maven {
|
||||
// Google Maven Repository
|
||||
|
@ -11,6 +12,7 @@ buildscript {
|
|||
classpath 'com.android.tools.build:gradle:3.1.+'
|
||||
classpath 'com.google.gms:google-services:3.0.0'
|
||||
classpath 'com.github.ksoichiro:gradle-eclipse-aar-plugin:0.3.1'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
include ':OsmAnd-java', ':OsmAndCore-sample'
|
||||
include ':OsmAnd-java', ':OsmAndCore-sample', ':OsmAnd-telegram'
|
||||
include ':OsmAnd'
|
||||
|
|