Browse Source

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

yattoz 4 years ago
parent
commit
459e044f06

+ 1 - 0
app/build.gradle View File

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

+ 33 - 1
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsFragment.kt View File

@@ -1,10 +1,13 @@
1 1
 package fr.forum_thalie.tsumugi.ui.news
2 2
 
3
+import android.os.Build
3 4
 import android.os.Bundle
4 5
 import android.util.Log
5 6
 import android.view.LayoutInflater
6 7
 import android.view.View
7 8
 import android.view.ViewGroup
9
+import android.view.WindowManager
10
+import android.webkit.WebView
8 11
 import androidx.fragment.app.Fragment
9 12
 import androidx.lifecycle.ViewModelProviders
10 13
 import androidx.recyclerview.widget.LinearLayoutManager
@@ -23,10 +26,39 @@ class NewsFragment : Fragment() {
23 26
         container: ViewGroup?,
24 27
         savedInstanceState: Bundle?
25 28
     ): View? {
29
+        var root = inflater.inflate(R.layout.fragment_news, container, false) as View
30
+        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
31
+        {
32
+            root = inflater.inflate(R.layout.fragment_news, container, false) as androidx.coordinatorlayout.widget.CoordinatorLayout
33
+            activity?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
34
+            val webView = WebView(context)
35
+
36
+            newsViewModel =
37
+                ViewModelProviders.of(this).get(NewsViewModel::class.java)
38
+
39
+            if (!newsViewModel.isWebViewLoaded)
40
+            {
41
+                try {
42
+                    val webViewS = root.findViewById<WebView>(R.id.news_webview)
43
+                    val webViewChat = WebViewNews(webViewS as WebView)
44
+                    webViewChat.start()
45
+                } catch (e: Exception) {
46
+                    root = inflater.inflate(R.layout.fragment_error_chat, container, false)
47
+                }
48
+
49
+                newsViewModel.isWebViewLoaded = true
50
+                Log.d(tag, "webview created")
51
+            } else {
52
+                Log.d(tag, "webview already created!?")
53
+            }
54
+
55
+            return root
56
+        }
57
+
26 58
         newsViewModel =
27 59
                 ViewModelProviders.of(this).get(NewsViewModel::class.java)
28 60
 
29
-        val root = inflater.inflate(R.layout.fragment_news, container, false) as androidx.swiperefreshlayout.widget.SwipeRefreshLayout
61
+        root = inflater.inflate(R.layout.fragment_news, container, false) as androidx.swiperefreshlayout.widget.SwipeRefreshLayout
30 62
 
31 63
         viewManager = LinearLayoutManager(context)
32 64
         viewAdapter = NewsAdapter(newsViewModel.newsArray, context!!)

+ 6 - 5
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsViewModel.kt View File

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

+ 47 - 0
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/WebViewNews.kt View File

@@ -0,0 +1,47 @@
1
+package fr.forum_thalie.tsumugi.ui.news
2
+
3
+import android.annotation.SuppressLint
4
+import android.content.Intent
5
+import android.net.Uri
6
+import android.webkit.WebChromeClient
7
+import android.webkit.WebView
8
+
9
+class WebViewNews(private val webView: WebView) {
10
+
11
+    @SuppressLint("SetJavaScriptEnabled")
12
+    fun start() {
13
+
14
+        val webSetting = this.webView.settings
15
+        webSetting.javaScriptEnabled = true
16
+        webSetting.setSupportZoom(true)
17
+
18
+        /* TODO: in the future, it could be nice to have a parameters screen where you can:
19
+     - Set the text zoom
20
+     - Set your username (to not type it every time, would it be possible?)
21
+     - Hide the chat?
22
+     - do more? */
23
+        webSetting.textZoom = 90
24
+
25
+        webSetting.setSupportMultipleWindows(true)
26
+        // needs to open target="_blank" links as KiwiIRC links have this attribute.
27
+        // shamelessly ripped off https://stackoverflow.com/questions/18187714/android-open-target-blank-links-in-webview-with-external-browser
28
+        this.webView.webChromeClient = object : WebChromeClient() {
29
+            override fun onCreateWindow(
30
+                view: WebView,
31
+                dialog: Boolean,
32
+                userGesture: Boolean,
33
+                resultMsg: android.os.Message
34
+            ): Boolean {
35
+                val result = view.hitTestResult
36
+                val data = result.extra
37
+                val context = view.context
38
+                val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(data))
39
+                context.startActivity(browserIntent)
40
+                return false
41
+            }
42
+        }
43
+
44
+        webView.loadUrl("https://tsumugi.forum-thalie.fr/")
45
+    }
46
+
47
+}

+ 1 - 1
app/src/main/res/layout/fragment_chat.xml View File

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

+ 34 - 8
app/src/main/res/layout/fragment_news.xml View File

@@ -1,16 +1,42 @@
1 1
 <?xml version="1.0" encoding="utf-8"?>
2
-<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
2
+<!--
3
+<androidx.constraintlayout.widget.ConstraintLayout
3 4
     xmlns:android="http://schemas.android.com/apk/res/android"
4 5
     xmlns:app="http://schemas.android.com/apk/res-auto"
5 6
     android:layout_width="match_parent"
6 7
     android:layout_height="match_parent" >
7 8
 
8
-    <androidx.recyclerview.widget.RecyclerView
9
+    <TextView
10
+        android:id="@+id/text_notifications"
9 11
         android:layout_width="match_parent"
10
-        android:layout_height="match_parent"
11
-        android:id="@+id/news_recycler"
12
-        android:scrollbars="vertical"
13
-        >
12
+        android:layout_height="wrap_content"
13
+        android:layout_marginStart="8dp"
14
+        android:layout_marginTop="8dp"
15
+        android:layout_marginEnd="8dp"
16
+        android:textAlignment="center"
17
+        android:textSize="20sp"
18
+        app:layout_constraintEnd_toEndOf="parent"
19
+        app:layout_constraintStart_toStartOf="parent"
20
+        app:layout_constraintTop_toTopOf="parent"/>
21
+</androidx.constraintlayout.widget.ConstraintLayout>
22
+-->
14 23
 
15
-    </androidx.recyclerview.widget.RecyclerView>
16
-</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
24
+<androidx.coordinatorlayout.widget.CoordinatorLayout
25
+    xmlns:android="http://schemas.android.com/apk/res/android"
26
+    xmlns:app="http://schemas.android.com/apk/res-auto"
27
+    android:layout_width="match_parent"
28
+    android:layout_height="match_parent"
29
+    >
30
+
31
+    <!--
32
+    Yattoz - this is a simple embedded WebView to display KiwiIRC in the app.
33
+    KiwiIRC is a decent IRC, but the website is clunky and you must scroll a lot.
34
+    This WebView is just the size of the screen, to make it practical.
35
+    -->
36
+
37
+    <WebView
38
+        android:id="@+id/news_webview"
39
+        android:layout_width="match_parent"
40
+        android:layout_height="match_parent" />
41
+
42
+</androidx.coordinatorlayout.widget.CoordinatorLayout>

+ 1 - 1
build.gradle View File

@@ -18,7 +18,7 @@ allprojects {
18 18
     repositories {
19 19
         google()
20 20
         jcenter()
21
-        
21
+        maven { url 'https://jitpack.io' }
22 22
     }
23 23
 }
24 24