From fcb065f0027783af19e3dc1fd4531fb9238f91bc Mon Sep 17 00:00:00 2001 From: sakacyber Date: Thu, 24 Nov 2022 23:22:25 +0700 Subject: [PATCH] add text color --- README.md | 6 +-- app/build.gradle | 12 +++--- .../android/sample/ExampleInstrumentedTest.kt | 6 +-- app/src/main/AndroidManifest.xml | 3 +- .../com/sakacyber/android/sample/Content.kt | 2 +- .../sakacyber/android/sample/MainActivity.kt | 2 +- app/src/main/res/layout/activity_main.xml | 3 +- .../android/sample/ExampleUnitTest.kt | 3 +- build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- html-textview/build.gradle | 41 +++++++++---------- .../htmltextview/ExampleInstrumentedTest.kt | 6 +-- html-textview/src/main/AndroidManifest.xml | 3 +- .../saka/android/htmltextview/HtmlTextView.kt | 11 +++-- .../htmltextview/activity/VideoActivity.kt | 30 +++++++------- .../callback/ClickEventCallback.kt | 4 +- .../htmltextview/element/BlockQuoteView.kt | 4 +- .../htmltextview/element/HeaderView.kt | 4 +- .../htmltextview/element/IFrameView.kt | 21 +++++----- .../htmltextview/element/ParagraphView.kt | 12 ++++-- .../android/htmltextview/element/WebView.kt | 4 +- .../saka/android/htmltextview/utility/Conf.kt | 11 ++--- .../android/htmltextview/utility/EManager.kt | 30 +++++++------- .../android/htmltextview/utility/Extension.kt | 2 +- .../htmltextview/utility/MyActivity.kt | 38 ++++++++--------- .../htmltextview/utility/ThumbnailLoader.kt | 7 ++-- .../htmltextview/utility/VideoLoader.kt | 10 +++-- .../android/htmltextview/ExampleUnitTest.kt | 3 +- 28 files changed, 146 insertions(+), 136 deletions(-) diff --git a/README.md b/README.md index c6a1a63..1505804 100644 --- a/README.md +++ b/README.md @@ -33,12 +33,12 @@ It currently supports tags : * `````` ## Quick Start -**HtmlTextView** is availble on jitpack. +**HtmlTextView** is available on jitpack. Add dependency: ``` -implementation "com.github.sakacyber:html-textview:1.0.13" +implementation "com.github.sakacyber:html-textview:1.0.14" ``` ## Usage @@ -53,5 +53,5 @@ to use **HtmlTextView**: android:padding="16dp" /> // activity or fragment -htmlTextView.setText(html) +htmlTextView.setText(html, lifecycleScope) ``` diff --git a/app/build.gradle b/app/build.gradle index 3141d29..65a20cc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,16 +1,18 @@ apply plugin: "com.android.application" apply plugin: "kotlin-android" +def versionNameStr = "1.0.14" + android { - compileSdkVersion 31 + compileSdkVersion 33 defaultConfig { applicationId "com.sakacyber.android.sample" minSdkVersion 23 - targetSdkVersion 31 - versionCode 13 - versionName "1.0.13" + targetSdkVersion 33 + versionCode 14 + versionName versionNameStr testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -30,7 +32,7 @@ android { proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" } } - + namespace "com.sakacyber.android.sample" } dependencies { diff --git a/app/src/androidTest/java/com/sakacyber/android/sample/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/sakacyber/android/sample/ExampleInstrumentedTest.kt index a1a7850..1bafbe5 100644 --- a/app/src/androidTest/java/com/sakacyber/android/sample/ExampleInstrumentedTest.kt +++ b/app/src/androidTest/java/com/sakacyber/android/sample/ExampleInstrumentedTest.kt @@ -1,13 +1,11 @@ package com.sakacyber.android.sample import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - +import androidx.test.runner.AndroidJUnit4 +import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith -import org.junit.Assert.* - /** * Instrumented test, which will execute on an Android device. * diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 57a2820..b7a314a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,5 @@ - + diff --git a/app/src/main/java/com/sakacyber/android/sample/Content.kt b/app/src/main/java/com/sakacyber/android/sample/Content.kt index 1eafe01..299635b 100644 --- a/app/src/main/java/com/sakacyber/android/sample/Content.kt +++ b/app/src/main/java/com/sakacyber/android/sample/Content.kt @@ -37,4 +37,4 @@ class Content { return sb.toString() } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/sakacyber/android/sample/MainActivity.kt b/app/src/main/java/com/sakacyber/android/sample/MainActivity.kt index 6f05fe3..eb60ae8 100644 --- a/app/src/main/java/com/sakacyber/android/sample/MainActivity.kt +++ b/app/src/main/java/com/sakacyber/android/sample/MainActivity.kt @@ -1,7 +1,7 @@ package com.sakacyber.android.sample -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.lifecycleScope import com.saka.android.htmltextview.HtmlTextView diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 57d08d7..547df71 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -18,7 +18,8 @@ android:id="@+id/htmlTextView" android:layout_width="match_parent" android:layout_height="match_parent" - android:padding="16dp" /> + android:padding="16dp" + app:htmlTextColor="@color/colorPrimary" /> diff --git a/app/src/test/java/com/sakacyber/android/sample/ExampleUnitTest.kt b/app/src/test/java/com/sakacyber/android/sample/ExampleUnitTest.kt index f74c8fa..c41a13d 100644 --- a/app/src/test/java/com/sakacyber/android/sample/ExampleUnitTest.kt +++ b/app/src/test/java/com/sakacyber/android/sample/ExampleUnitTest.kt @@ -1,9 +1,8 @@ package com.sakacyber.android.sample +import org.junit.Assert.assertEquals import org.junit.Test -import org.junit.Assert.* - /** * Example local unit test, which will execute on the development machine (host). * diff --git a/build.gradle b/build.gradle index b0c9e50..30fd7db 100644 --- a/build.gradle +++ b/build.gradle @@ -19,7 +19,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:7.2.1' + classpath "com.android.tools.build:gradle:7.3.1" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" classpath "com.github.dcendents:android-maven-gradle-plugin:2.1" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2e837ab..995a433 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip diff --git a/html-textview/build.gradle b/html-textview/build.gradle index 3eeefb4..92defdd 100644 --- a/html-textview/build.gradle +++ b/html-textview/build.gradle @@ -1,8 +1,8 @@ -apply plugin: 'com.android.library' -apply plugin: 'kotlin-android' -apply plugin: 'kotlin-kapt' +apply plugin: "com.android.library" +apply plugin: "kotlin-android" +apply plugin: "kotlin-kapt" -group = 'com.github.SakaGamer' +group = "com.github.SakaGamer" android { @@ -11,11 +11,9 @@ android { defaultConfig { minSdkVersion 21 targetSdkVersion 31 - versionCode 1 - versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - consumerProguardFiles 'consumer-rules.pro' + consumerProguardFiles "consumer-rules.pro" } compileOptions { @@ -30,32 +28,33 @@ android { buildTypes { release { minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" } } + namespace "com.saka.android.htmltextview" } dependencies { - implementation 'androidx.appcompat:appcompat:1.4.1' - implementation 'androidx.core:core-ktx:1.7.0' + implementation "androidx.appcompat:appcompat:1.4.1" + implementation "androidx.core:core-ktx:1.7.0" implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1" // java HTML parser - implementation 'org.jsoup:jsoup:1.13.1' + implementation "org.jsoup:jsoup:1.13.1" // image loader - implementation 'com.github.bumptech.glide:glide:4.12.0' - kapt 'com.github.bumptech.glide:compiler:4.12.0' + implementation "com.github.bumptech.glide:glide:4.12.0" + kapt "com.github.bumptech.glide:compiler:4.12.0" - implementation 'com.google.android.exoplayer:exoplayer:2.17.1' - implementation 'com.github.stfalcon-studio:StfalconImageViewer:v1.0.1' - implementation 'io.github.muddz:quickshot:1.4.0' - implementation 'com.karumi:dexter:6.2.2' + implementation "com.google.android.exoplayer:exoplayer:2.18.2" + implementation "com.github.stfalcon-studio:StfalconImageViewer:v1.0.1" + implementation "io.github.muddz:quickshot:1.4.0" + implementation "com.karumi:dexter:6.2.2" - testImplementation 'junit:junit:4.13.2' - androidTestImplementation 'androidx.test.ext:junit:1.1.3' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + testImplementation "junit:junit:4.13.2" + androidTestImplementation "androidx.test.ext:junit:1.1.3" + androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0" - implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:11.0.1' + implementation "com.pierfrancescosoffritti.androidyoutubeplayer:core:11.0.1" } diff --git a/html-textview/src/androidTest/java/com/saka/android/htmltextview/ExampleInstrumentedTest.kt b/html-textview/src/androidTest/java/com/saka/android/htmltextview/ExampleInstrumentedTest.kt index 89ad64f..2e38844 100644 --- a/html-textview/src/androidTest/java/com/saka/android/htmltextview/ExampleInstrumentedTest.kt +++ b/html-textview/src/androidTest/java/com/saka/android/htmltextview/ExampleInstrumentedTest.kt @@ -1,13 +1,11 @@ package com.saka.android.htmltextview -import androidx.test.platform.app.InstrumentationRegistry import androidx.test.ext.junit.runners.AndroidJUnit4 - +import androidx.test.platform.app.InstrumentationRegistry +import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith -import org.junit.Assert.* - /** * Instrumented test, which will execute on an Android device. * diff --git a/html-textview/src/main/AndroidManifest.xml b/html-textview/src/main/AndroidManifest.xml index ada36bd..1feecf5 100644 --- a/html-textview/src/main/AndroidManifest.xml +++ b/html-textview/src/main/AndroidManifest.xml @@ -1,6 +1,5 @@ - + diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/HtmlTextView.kt b/html-textview/src/main/java/com/saka/android/htmltextview/HtmlTextView.kt index d8893db..29f16c2 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/HtmlTextView.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/HtmlTextView.kt @@ -3,6 +3,7 @@ package com.saka.android.htmltextview import android.content.Context import android.util.AttributeSet import android.widget.LinearLayout +import androidx.core.content.ContextCompat import androidx.core.widget.NestedScrollView import androidx.lifecycle.LifecycleCoroutineScope import com.saka.android.htmltextview.utility.Conf @@ -13,9 +14,9 @@ import org.jsoup.Jsoup class HtmlTextView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, - defStyleAttr: Int = 0, + defStyleAttr: Int = 0 ) : NestedScrollView(context, attrs, defStyleAttr) { - + init { val typeArray = context.obtainStyledAttributes( attrs, @@ -25,6 +26,10 @@ class HtmlTextView @JvmOverloads constructor( R.styleable.HtmlTextView_htmlTextSize, Conf.DEF_TEXT_SIZE ) + Conf.textColor = typeArray.getColor( + R.styleable.HtmlTextView_htmlTextColor, + ContextCompat.getColor(context, R.color.colorTextPrimary) + ) Conf.textLineSpacing = typeArray.getDimension( R.styleable.HtmlTextView_htmlLineSpacing, Conf.DEF_LINE_SPACING @@ -40,7 +45,7 @@ class HtmlTextView @JvmOverloads constructor( htmlContent = typeArray.getString(R.styleable.HtmlTextView_htmlContent) ?: "" typeArray.recycle() } - + fun setText(html: String?, lifecycle: LifecycleCoroutineScope) { htmlContent = html ?: return val children = Jsoup.parse(htmlContent).body().children() diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/activity/VideoActivity.kt b/html-textview/src/main/java/com/saka/android/htmltextview/activity/VideoActivity.kt index a243763..e985d54 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/activity/VideoActivity.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/activity/VideoActivity.kt @@ -20,16 +20,16 @@ import com.saka.android.htmltextview.R import com.saka.android.htmltextview.utility.VideoLoader class VideoActivity : AppCompatActivity() { - + private var player: ExoPlayer? = null - + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN) setContentView(R.layout.activity_video) val toolbar = findViewById(R.id.toolbar) setSupportActionBar(toolbar) - + val youTubePlayerView = findViewById(R.id.youtube_player_view) val playerView = findViewById(R.id.playerView) val webView = findViewById(R.id.webView) @@ -47,20 +47,22 @@ class VideoActivity : AppCompatActivity() { } } } - + private fun loadExoPlayer(link: String, playerView: StyledPlayerView, toolbar: Toolbar) { // create player view playerView.setShowBuffering(StyledPlayerView.SHOW_BUFFERING_WHEN_PLAYING) - playerView.setControllerVisibilityListener { visibility -> - toolbar.visibility = visibility - } - + playerView.setControllerVisibilityListener( + StyledPlayerView.ControllerVisibilityListener { + toolbar.visibility = it + } + ) + // create media source val dataSourceFactory = DefaultDataSource.Factory(this) val mediaItem = MediaItem.fromUri(link) val mediaSource = ProgressiveMediaSource.Factory(dataSourceFactory) .createMediaSource(mediaItem) - + // create player player = ExoPlayer.Builder(this).build() player?.setMediaSource(mediaSource) @@ -68,7 +70,7 @@ class VideoActivity : AppCompatActivity() { player?.playWhenReady = true playerView.player = player } - + private fun loadYoutubePlayer( videoId: String?, playerView: YouTubePlayerView, @@ -83,7 +85,7 @@ class VideoActivity : AppCompatActivity() { } }) } - + @SuppressLint("SetJavaScriptEnabled") private fun loadIFrameWebView(link: String, webView: WebView) { webView.settings.javaScriptEnabled = true @@ -93,19 +95,19 @@ class VideoActivity : AppCompatActivity() { ) webView.loadData(encodedHtml, "text/html", "base64") } - + override fun onSupportNavigateUp(): Boolean { finish() return super.onSupportNavigateUp() } - + override fun onPause() { super.onPause() if (player != null) { player?.playWhenReady = false } } - + override fun onDestroy() { super.onDestroy() if (player != null) { diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/callback/ClickEventCallback.kt b/html-textview/src/main/java/com/saka/android/htmltextview/callback/ClickEventCallback.kt index e5464d8..f11fab0 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/callback/ClickEventCallback.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/callback/ClickEventCallback.kt @@ -1,6 +1,6 @@ package com.saka.android.htmltextview.callback interface ClickEventCallback { - fun onHtmlImageClick(pos: Int, src: String?){} - fun onHtmlVideoClick(pos: Int, src: String?){} + fun onHtmlImageClick(pos: Int, src: String?) {} + fun onHtmlVideoClick(pos: Int, src: String?) {} } diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/element/BlockQuoteView.kt b/html-textview/src/main/java/com/saka/android/htmltextview/element/BlockQuoteView.kt index 44902d4..d2bfb7e 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/element/BlockQuoteView.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/element/BlockQuoteView.kt @@ -16,7 +16,7 @@ class BlockQuoteView @JvmOverloads constructor( content: String = "", coroutineScope: CoroutineScope? = null ) : BaseElement(context, attrs, defStyleAttr, element, content, coroutineScope) { - + override fun render() { orientation = VERTICAL layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) @@ -31,7 +31,7 @@ class BlockQuoteView @JvmOverloads constructor( } setText() } - + private fun setText() { val paragraphView = TextView(context) paragraphView.setTypeface(paragraphView.typeface, Typeface.ITALIC) diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/element/HeaderView.kt b/html-textview/src/main/java/com/saka/android/htmltextview/element/HeaderView.kt index f8535da..b2d4a39 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/element/HeaderView.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/element/HeaderView.kt @@ -15,13 +15,13 @@ class HeaderView @JvmOverloads constructor( content: String = "", coroutineScope: CoroutineScope? = null ) : BaseElement(context, attrs, defStyleAttr, element, content, coroutineScope) { - + override fun render() { orientation = VERTICAL layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) setContent() } - + @SuppressLint("SetJavaScriptEnabled") private fun setContent() { val webView = WebViewCompat(context) diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/element/IFrameView.kt b/html-textview/src/main/java/com/saka/android/htmltextview/element/IFrameView.kt index 1cd9ee4..1df8c91 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/element/IFrameView.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/element/IFrameView.kt @@ -30,18 +30,18 @@ class IFrameView @JvmOverloads constructor( content: String = "", coroutineScope: CoroutineScope? = null ) : BaseElement(context, attrs, defStyleAttr, element, content, coroutineScope) { - + private var isInProgress = false - + companion object { const val BASE_URL = "http://13.250.71.15:81" } - + override fun render() { orientation = VERTICAL setContent() } - + @SuppressLint("ClickableViewAccessibility") private fun setContent() { link?.let { @@ -52,7 +52,7 @@ class IFrameView @JvmOverloads constructor( imagePreview.setImageDrawable( ContextCompat.getDrawable(context, R.drawable.img_holder) ) - + scope?.launch { it.loadBitmap { bitmap -> imagePreview.background = BitmapDrawable(Resources.getSystem(), bitmap) @@ -61,12 +61,13 @@ class IFrameView @JvmOverloads constructor( ContextCompat.getDrawable( imagePreview.context, R.drawable.ic_play_circle_24 - ), 80 + ), + 80 ) ) } } - + addView(imagePreview) setOnClickListener { startVideoPlayer() @@ -122,7 +123,7 @@ class IFrameView @JvmOverloads constructor( } } } - + private fun startVideoPlayer() { if (isInProgress) return isInProgress = true @@ -130,12 +131,12 @@ class IFrameView @JvmOverloads constructor( .putExtra("link", link) ContextCompat.startActivity(context, intent, null) } - + override fun onWindowFocusChanged(hasWindowFocus: Boolean) { super.onWindowFocusChanged(hasWindowFocus) if (hasWindowFocus) isInProgress = false } - + private val link: String? get() = element?.attr("abs:src") } diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/element/ParagraphView.kt b/html-textview/src/main/java/com/saka/android/htmltextview/element/ParagraphView.kt index 08017e4..996d1f3 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/element/ParagraphView.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/element/ParagraphView.kt @@ -21,7 +21,7 @@ class ParagraphView @JvmOverloads constructor( content: String = "", coroutineScope: CoroutineScope? = null ) : BaseElement(context, attrs, defStyleAttr, element, content, coroutineScope) { - + override fun render() { orientation = VERTICAL layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) @@ -36,11 +36,17 @@ class ParagraphView @JvmOverloads constructor( } setText() } - + private fun setText() { val paragraphView = TextView(context) paragraphView.autoLinkMask = Linkify.ALL - paragraphView.setTextColor(ContextCompat.getColor(context, R.color.colorTextPrimary)) + paragraphView.setTextColor( + if (Conf.textColor != 0) { + Conf.textColor + } else { + ContextCompat.getColor(context, R.color.colorTextPrimary) + } + ) paragraphView.typeface = ResourcesCompat.getFont(context, R.font.font_battambang_regular) // val paragraphView = View.inflate(context, R.layout.paragraph_view, null) as TextView // paragraphView.setTextSize(TypedValue.COMPLEX_UNIT_PX, Conf.textSize) diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/element/WebView.kt b/html-textview/src/main/java/com/saka/android/htmltextview/element/WebView.kt index 45ee19e..fe5c32e 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/element/WebView.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/element/WebView.kt @@ -15,13 +15,13 @@ class WebView @JvmOverloads constructor( content: String = "", coroutineScope: CoroutineScope? = null ) : BaseElement(context, attrs, defStyleAttr, element, content, coroutineScope) { - + override fun render() { orientation = VERTICAL layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) setContent() } - + @SuppressLint("SetJavaScriptEnabled") private fun setContent() { val webView = WebViewCompat(context) diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/utility/Conf.kt b/html-textview/src/main/java/com/saka/android/htmltextview/utility/Conf.kt index 6ca53cc..dd16b7f 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/utility/Conf.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/utility/Conf.kt @@ -3,17 +3,18 @@ package com.saka.android.htmltextview.utility object Conf { // default - var DEF_TEXT_SIZE = 12f - var DEF_LINE_SPACING = 0f - var DEF_IMAGE_ROUND = 0f - var DEF_PADDING = 0f + const val DEF_TEXT_SIZE = 12f + const val DEF_TEXT_COLOR = 0 + const val DEF_LINE_SPACING = 0f + const val DEF_IMAGE_ROUND = 0f + const val DEF_PADDING = 0f // config var textSize = DEF_TEXT_SIZE var textLineSpacing = DEF_LINE_SPACING var imageRoundCorner = DEF_IMAGE_ROUND var textPadding = DEF_PADDING - + var textColor = DEF_TEXT_COLOR var htmlContent = "" private var imageList = mutableListOf() diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/utility/EManager.kt b/html-textview/src/main/java/com/saka/android/htmltextview/utility/EManager.kt index 75ed601..0097e58 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/utility/EManager.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/utility/EManager.kt @@ -16,12 +16,12 @@ import org.jsoup.Jsoup import org.jsoup.select.Elements object EManager { - + fun appendView( view: ViewGroup, elements: Elements, content: String = "", - coroutineScope: CoroutineScope?, + coroutineScope: CoroutineScope? ) { var child: BaseElement for (e in elements) { @@ -31,7 +31,7 @@ object EManager { context = view.context, element = e, content = content, - coroutineScope = coroutineScope, + coroutineScope = coroutineScope ) } "h" -> { @@ -39,7 +39,7 @@ object EManager { context = view.context, element = e, content = content, - coroutineScope = coroutineScope, + coroutineScope = coroutineScope ) } "iframe" -> { @@ -47,7 +47,7 @@ object EManager { context = view.context, element = e, content = content, - coroutineScope = coroutineScope, + coroutineScope = coroutineScope ) } "p" -> { @@ -55,7 +55,7 @@ object EManager { context = view.context, element = e, content = content, - coroutineScope = coroutineScope, + coroutineScope = coroutineScope ) } "img" -> { @@ -63,7 +63,7 @@ object EManager { context = view.context, element = e, content = content, - coroutineScope = coroutineScope, + coroutineScope = coroutineScope ) } "div" -> { @@ -71,7 +71,7 @@ object EManager { context = view.context, element = e, content = content, - coroutineScope = coroutineScope, + coroutineScope = coroutineScope ) } "table" -> { @@ -79,7 +79,7 @@ object EManager { context = view.context, element = e, content = content, - coroutineScope = coroutineScope, + coroutineScope = coroutineScope ) } "ul" -> { @@ -87,7 +87,7 @@ object EManager { context = view.context, element = e, content = content, - coroutineScope = coroutineScope, + coroutineScope = coroutineScope ) } "ol" -> { @@ -95,7 +95,7 @@ object EManager { context = view.context, element = e, content = content, - coroutineScope = coroutineScope, + coroutineScope = coroutineScope ) } else -> { @@ -103,19 +103,19 @@ object EManager { context = view.context, element = e, content = content, - coroutineScope = coroutineScope, + coroutineScope = coroutineScope ) } } view.addView(child) } } - + fun dpToPx(context: Context, dp: Int): Float { val displayMetrics = context.resources.displayMetrics return (dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)) } - + fun findAllVideoLinks(content: String?): List { content ?: return emptyList() val links: MutableList = ArrayList() @@ -128,7 +128,7 @@ object EManager { } return links } - + fun findAllImagesLinks(content: String?): List { content ?: return emptyList() val links: MutableList = ArrayList() diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/utility/Extension.kt b/html-textview/src/main/java/com/saka/android/htmltextview/utility/Extension.kt index 2ec3431..956c90d 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/utility/Extension.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/utility/Extension.kt @@ -45,4 +45,4 @@ fun String.findAllImageLinks(): List { } } return links -} \ No newline at end of file +} diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/utility/MyActivity.kt b/html-textview/src/main/java/com/saka/android/htmltextview/utility/MyActivity.kt index c852a10..c970e68 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/utility/MyActivity.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/utility/MyActivity.kt @@ -15,24 +15,24 @@ import com.saka.android.htmltextview.R.id import com.saka.android.htmltextview.R.layout class MyActivity : Activity() { - + private var webView: WebView? = null private var customViewContainer: FrameLayout? = null private var customViewCallback: CustomViewCallback? = null private var mCustomView: View? = null private var mWebChromeClient: MyWebChromeClient? = null - + @SuppressLint("SetJavaScriptEnabled") public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(layout.main) customViewContainer = findViewById(id.customViewContainer) webView = findViewById(id.webView) - + val webView = webView ?: return val link = intent.getStringExtra("link") val mWebViewClient = MyWebViewClient() - + webView.webViewClient = mWebViewClient mWebChromeClient = MyWebChromeClient() webView.webChromeClient = mWebChromeClient @@ -64,32 +64,32 @@ class MyActivity : Activity() { """ webView.loadData(url, "text/html", "UTF-8") } - + private fun inCustomView(): Boolean { return mCustomView != null } - + private fun hideCustomView() { mWebChromeClient?.onHideCustomView() } - + override fun onPause() { super.onPause() webView?.onPause() } - + override fun onResume() { super.onResume() webView?.onResume() } - + override fun onStop() { super.onStop() if (inCustomView()) { hideCustomView() } } - + override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { if (keyCode == KeyEvent.KEYCODE_BACK) { if (inCustomView()) { @@ -103,12 +103,12 @@ class MyActivity : Activity() { } return super.onKeyDown(keyCode, event) } - + internal inner class MyWebChromeClient : WebChromeClient() { - + // private val mDefaultVideoPoster: Bitmap? = null private var mVideoProgressView: View? = null - + override fun onShowCustomView(view: View, callback: CustomViewCallback) { // if a view already exists then immediately terminate the new one if (mCustomView != null) { @@ -121,7 +121,7 @@ class MyActivity : Activity() { customViewContainer?.addView(view) customViewCallback = callback } - + override fun getVideoLoadingProgressView(): View? { if (mVideoProgressView == null) { val inflater = LayoutInflater.from(this@MyActivity) @@ -129,23 +129,23 @@ class MyActivity : Activity() { } return mVideoProgressView } - + override fun onHideCustomView() { super.onHideCustomView() - + if (mCustomView == null) return webView?.visibility = View.VISIBLE customViewContainer?.visibility = View.GONE - + // Hide the custom view. mCustomView?.visibility = View.GONE - + // Remove the custom view from its container. customViewContainer?.removeView(mCustomView) customViewCallback?.onCustomViewHidden() mCustomView = null } } - + internal class MyWebViewClient : WebViewClient() } diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/utility/ThumbnailLoader.kt b/html-textview/src/main/java/com/saka/android/htmltextview/utility/ThumbnailLoader.kt index c140a78..b94f624 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/utility/ThumbnailLoader.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/utility/ThumbnailLoader.kt @@ -3,7 +3,6 @@ package com.saka.android.htmltextview.utility import android.graphics.Bitmap import android.graphics.BitmapFactory import android.media.MediaMetadataRetriever -import android.util.Log import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -11,7 +10,7 @@ import kotlinx.coroutines.withContext import java.net.URL suspend fun String?.loadBitmap( - onPostExecute: (Bitmap?) -> Unit, + onPostExecute: (Bitmap?) -> Unit ) { val url = this ?: return var bitmap: Bitmap? = null @@ -46,7 +45,7 @@ suspend fun String?.loadBitmap( fun CoroutineScope.executeAsyncTask( onPreExecute: () -> Unit, doInBackground: () -> R, - onPostExecute: (R) -> Unit, + onPostExecute: (R) -> Unit ) = launch { onPreExecute() val result = @@ -54,4 +53,4 @@ fun CoroutineScope.executeAsyncTask( doInBackground() } onPostExecute(result) -} \ No newline at end of file +} diff --git a/html-textview/src/main/java/com/saka/android/htmltextview/utility/VideoLoader.kt b/html-textview/src/main/java/com/saka/android/htmltextview/utility/VideoLoader.kt index e8f7f05..427428a 100644 --- a/html-textview/src/main/java/com/saka/android/htmltextview/utility/VideoLoader.kt +++ b/html-textview/src/main/java/com/saka/android/htmltextview/utility/VideoLoader.kt @@ -4,11 +4,11 @@ import java.util.regex.Matcher import java.util.regex.Pattern object VideoLoader { - + fun isYoutubeFormat(it: String): Boolean { return it.contains("youtube.com/") || it.contains("youtu.be/") } - + fun getYouTubeId(youTubeUrl: String?): String? { val pattern = "https?://(?:[0-9A-Z-]+\\.)?(?:youtu\\.be/|youtube\\.com\\S*[^\\w\\-\\s])([\\w\\-]{11})(?=[^\\w\\-]|$)(?![?=&+%\\w]*(?:['\"][^<>]*>|))[?=&+%\\w]*" @@ -19,9 +19,11 @@ object VideoLoader { val matcher: Matcher = compiledPattern.matcher(youTubeUrl) return if (matcher.find()) { matcher.group(1) - } else null + } else { + null + } } - + fun generateIFrameEncode(link: String): String { val htmlStart = """ diff --git a/html-textview/src/test/java/com/saka/android/htmltextview/ExampleUnitTest.kt b/html-textview/src/test/java/com/saka/android/htmltextview/ExampleUnitTest.kt index 533deb7..b0fc5d6 100644 --- a/html-textview/src/test/java/com/saka/android/htmltextview/ExampleUnitTest.kt +++ b/html-textview/src/test/java/com/saka/android/htmltextview/ExampleUnitTest.kt @@ -1,9 +1,8 @@ package com.saka.android.htmltextview +import org.junit.Assert.assertEquals import org.junit.Test -import org.junit.Assert.* - /** * Example local unit test, which will execute on the development machine (host). *