diff --git a/OsmAnd-telegram/.gitignore b/OsmAnd-telegram/.gitignore index 7ef6f23e16..540de9ded1 100644 --- a/OsmAnd-telegram/.gitignore +++ b/OsmAnd-telegram/.gitignore @@ -12,6 +12,7 @@ obj/ out/ use/ src/org/drinkless/td/ +assets/fonts/* # Android Studio /.idea diff --git a/OsmAnd-telegram/build.gradle b/OsmAnd-telegram/build.gradle index 3355892eb1..fdc07cacb8 100644 --- a/OsmAnd-telegram/build.gradle +++ b/OsmAnd-telegram/build.gradle @@ -111,10 +111,19 @@ task downloadPrebuiltTelegram { dependsOn downloadTdLibzip, copyNativeLibs, copyJavaSources } +task collectFonts(type: Copy) { + from "../../resources/fonts" + into "assets/fonts" + include "*.ttf" +} + +task collectExternalResources { + dependsOn collectFonts +} afterEvaluate { android.applicationVariants.all { variant -> - variant.javaCompiler.dependsOn(downloadPrebuiltTelegram) + variant.javaCompiler.dependsOn(downloadPrebuiltTelegram, collectExternalResources) variant.preBuild.dependsOn(downloadPrebuiltTelegram) } } diff --git a/OsmAnd-telegram/res/values/attrs.xml b/OsmAnd-telegram/res/values/attrs.xml index f4c7b2187d..9726e41f4c 100644 --- a/OsmAnd-telegram/res/values/attrs.xml +++ b/OsmAnd-telegram/res/values/attrs.xml @@ -12,4 +12,8 @@ + + + + \ No newline at end of file diff --git a/OsmAnd-telegram/res/values/fonts.xml b/OsmAnd-telegram/res/values/fonts.xml new file mode 100644 index 0000000000..b6a1c3d72c --- /dev/null +++ b/OsmAnd-telegram/res/values/fonts.xml @@ -0,0 +1,5 @@ + + + fonts/Roboto-Regular.ttf + fonts/Roboto-Medium.ttf + diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/FontCache.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/FontCache.kt new file mode 100644 index 0000000000..4b17006ab6 --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/FontCache.kt @@ -0,0 +1,45 @@ +package net.osmand.telegram.helpers + +import android.content.Context +import android.graphics.Typeface +import android.util.Log +import java.util.concurrent.ConcurrentHashMap + +private const val TAG = "FontCache" +private const val ROBOTO_MEDIUM = "fonts/Roboto-Medium.ttf" +private const val ROBOTO_REGULAR = "fonts/Roboto-Regular.ttf" + +object FontCache { + + private val fontsMap = ConcurrentHashMap() + + fun getRobotoMedium(context: Context): Typeface? { + return getFont(context, ROBOTO_MEDIUM) + } + + fun getRobotoRegular(context: Context): Typeface? { + return getFont(context, ROBOTO_REGULAR) + } + + fun getFont(context: Context, fontName: String): Typeface? { + var typeface: Typeface? = fontsMap[fontName] + if (typeface != null) { + return typeface + } + + try { + typeface = Typeface.createFromAsset(context.assets, fontName) + } catch (e: Exception) { + Log.e(TAG, "Failed to create typeface from asset '$fontName'", e) + return null + } + + if (typeface == null) { + return null + } + + fontsMap[fontName] = typeface + + return typeface + } +} diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/views/TextViewEx.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/views/TextViewEx.kt new file mode 100644 index 0000000000..6d4998208b --- /dev/null +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/views/TextViewEx.kt @@ -0,0 +1,49 @@ +package net.osmand.telegram.ui.views + +import android.content.Context +import android.content.res.TypedArray +import android.support.v7.widget.AppCompatTextView +import android.util.AttributeSet +import net.osmand.telegram.R +import net.osmand.telegram.helpers.FontCache + +class TextViewEx : AppCompatTextView { + + constructor(context: Context) : super(context) + + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { + parseAttrs(attrs, 0) + } + + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( + context, + attrs, + defStyleAttr + ) { + parseAttrs(attrs, defStyleAttr) + } + + private fun parseAttrs(attrs: AttributeSet?, defStyleAttr: Int) { + if (attrs == null) { + return + } + + val resolvedAttrs = + context.theme.obtainStyledAttributes(attrs, R.styleable.TextViewEx, defStyleAttr, 0) + applyAttrTypeface(resolvedAttrs) + resolvedAttrs.recycle() + } + + private fun applyAttrTypeface(resolvedAttrs: TypedArray) { + if (isInEditMode || !resolvedAttrs.hasValue(R.styleable.TextViewEx_typeface)) { + return + } + + val typefaceName = resolvedAttrs.getString(R.styleable.TextViewEx_typeface) + val typeface = FontCache.getFont(context, typefaceName) + if (typeface != null) { + val style = getTypeface()?.style ?: 0 + setTypeface(typeface, style) + } + } +}