added fallback webView for API19- (no TLSv1.2 for RSS Parser)

remotes/origin/devel
yattoz 2019-12-08 23:42:22 +01:00
parent c8bf207ce5
commit 459e044f06
7 changed files with 123 additions and 16 deletions

View File

@ -77,6 +77,7 @@ dependencies {
// RSS parser library @ https://github.com/prof18/RSS-Parser // RSS parser library @ https://github.com/prof18/RSS-Parser
implementation 'com.prof.rssparser:rssparser:2.0.4' implementation 'com.prof.rssparser:rssparser:2.0.4'
implementation 'com.github.ahorn:android-rss:1.0-rc1'
def work_version = "2.2.0" def work_version = "2.2.0"
implementation "androidx.work:work-runtime-ktx:$work_version" implementation "androidx.work:work-runtime-ktx:$work_version"

View File

@ -1,10 +1,13 @@
package fr.forum_thalie.tsumugi.ui.news package fr.forum_thalie.tsumugi.ui.news
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.WindowManager
import android.webkit.WebView
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -23,10 +26,39 @@ class NewsFragment : Fragment() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
var root = inflater.inflate(R.layout.fragment_news, container, false) as View
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
{
root = inflater.inflate(R.layout.fragment_news, container, false) as androidx.coordinatorlayout.widget.CoordinatorLayout
activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
val webView = WebView(context)
newsViewModel =
ViewModelProviders.of(this).get(NewsViewModel::class.java)
if (!newsViewModel.isWebViewLoaded)
{
try {
val webViewS = root.findViewById<WebView>(R.id.news_webview)
val webViewChat = WebViewNews(webViewS as WebView)
webViewChat.start()
} catch (e: Exception) {
root = inflater.inflate(R.layout.fragment_error_chat, container, false)
}
newsViewModel.isWebViewLoaded = true
Log.d(tag, "webview created")
} else {
Log.d(tag, "webview already created!?")
}
return root
}
newsViewModel = newsViewModel =
ViewModelProviders.of(this).get(NewsViewModel::class.java) ViewModelProviders.of(this).get(NewsViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_news, container, false) as androidx.swiperefreshlayout.widget.SwipeRefreshLayout root = inflater.inflate(R.layout.fragment_news, container, false) as androidx.swiperefreshlayout.widget.SwipeRefreshLayout
viewManager = LinearLayoutManager(context) viewManager = LinearLayoutManager(context)
viewAdapter = NewsAdapter(newsViewModel.newsArray, context!!) viewAdapter = NewsAdapter(newsViewModel.newsArray, context!!)

View File

@ -1,5 +1,6 @@
package fr.forum_thalie.tsumugi.ui.news package fr.forum_thalie.tsumugi.ui.news
import android.os.Build
import android.util.Log import android.util.Log
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -23,6 +24,7 @@ import kotlin.math.min
class NewsViewModel : ViewModel() { class NewsViewModel : ViewModel() {
val newsArray : ArrayList<News> = ArrayList() val newsArray : ArrayList<News> = ArrayList()
var isWebViewLoaded = false
private val urlToScrape = "https://tsumugi.forum-thalie.fr/?feed=rss2" private val urlToScrape = "https://tsumugi.forum-thalie.fr/?feed=rss2"
@ -53,10 +55,12 @@ class NewsViewModel : ViewModel() {
fun fetch(root: androidx.swiperefreshlayout.widget.SwipeRefreshLayout? = null, viewAdapter: RecyclerView.Adapter<*>? = null) fun fetch(root: androidx.swiperefreshlayout.widget.SwipeRefreshLayout? = null, viewAdapter: RecyclerView.Adapter<*>? = null)
{ {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
return // the RSS Parser does not support API20- because of no TLS v1.2
val maxNumberOfArticles = 5 val maxNumberOfArticles = 5
coroutineScope.launch(Dispatchers.Main) { coroutineScope.launch(Dispatchers.Main) {
Log.d(tag, "launching coroutine") Log.d(tag, "launching coroutine")
try {
val parser = Parser() val parser = Parser()
val articleList = parser.getArticles(urlToScrape) val articleList = parser.getArticles(urlToScrape)
newsArray.clear() newsArray.clear()
@ -81,10 +85,7 @@ class NewsViewModel : ViewModel() {
} }
// The list contains all article's data. For example you can use it for your adapter. // The list contains all article's data. For example you can use it for your adapter.
root?.isRefreshing = false root?.isRefreshing = false
viewAdapter?.notifyDataSetChanged()
} catch (e: Exception) {
// Handle the exception
}
} }
} }
} }

View File

@ -0,0 +1,47 @@
package fr.forum_thalie.tsumugi.ui.news
import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.webkit.WebChromeClient
import android.webkit.WebView
class WebViewNews(private val webView: WebView) {
@SuppressLint("SetJavaScriptEnabled")
fun start() {
val webSetting = this.webView.settings
webSetting.javaScriptEnabled = true
webSetting.setSupportZoom(true)
/* TODO: in the future, it could be nice to have a parameters screen where you can:
- Set the text zoom
- Set your username (to not type it every time, would it be possible?)
- Hide the chat?
- do more? */
webSetting.textZoom = 90
webSetting.setSupportMultipleWindows(true)
// needs to open target="_blank" links as KiwiIRC links have this attribute.
// shamelessly ripped off https://stackoverflow.com/questions/18187714/android-open-target-blank-links-in-webview-with-external-browser
this.webView.webChromeClient = object : WebChromeClient() {
override fun onCreateWindow(
view: WebView,
dialog: Boolean,
userGesture: Boolean,
resultMsg: android.os.Message
): Boolean {
val result = view.hitTestResult
val data = result.extra
val context = view.context
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(data))
context.startActivity(browserIntent)
return false
}
}
webView.loadUrl("https://tsumugi.forum-thalie.fr/")
}
}

View File

@ -35,7 +35,7 @@ This WebView is just the size of the screen, to make it practical.
--> -->
<WebView <WebView
android:id="@+id/chat_webview" android:id="@+id/news_webview"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="match_parent" />

View File

@ -1,16 +1,42 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout <!--
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" > android:layout_height="match_parent" >
<androidx.recyclerview.widget.RecyclerView <TextView
android:id="@+id/text_notifications"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:id="@+id/news_recycler" android:layout_marginStart="8dp"
android:scrollbars="vertical" android:layout_marginTop="8dp"
> android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
-->
</androidx.recyclerview.widget.RecyclerView> <androidx.coordinatorlayout.widget.CoordinatorLayout
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout> xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--
Yattoz - this is a simple embedded WebView to display KiwiIRC in the app.
KiwiIRC is a decent IRC, but the website is clunky and you must scroll a lot.
This WebView is just the size of the screen, to make it practical.
-->
<WebView
android:id="@+id/news_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -18,7 +18,7 @@ allprojects {
repositories { repositories {
google() google()
jcenter() jcenter()
maven { url 'https://jitpack.io' }
} }
} }