Преглед изворни кода

Added image fetch in RSS (thanks stackoverflow!)

yattoz пре 4 година
родитељ
комит
d4f6064e69
1 измењених фајлова са 92 додато и 3 уклоњено
  1. 92 3
      app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsAdapter.kt

+ 92 - 3
app/src/main/java/fr/forum_thalie/tsumugi/ui/news/NewsAdapter.kt Прегледај датотеку

@@ -1,20 +1,91 @@
1 1
 package fr.forum_thalie.tsumugi.ui.news
2 2
 
3 3
 import android.annotation.SuppressLint
4
+import android.app.Activity
5
+import android.app.PendingIntent.getActivity
4 6
 import android.content.Context
5 7
 import android.content.Intent
8
+import android.graphics.Bitmap
9
+import android.graphics.BitmapFactory
10
+import android.graphics.Point
11
+import android.graphics.drawable.BitmapDrawable
12
+import android.graphics.drawable.Drawable
13
+import android.graphics.drawable.LevelListDrawable
6 14
 import android.net.Uri
7
-import android.widget.TextView
15
+import android.os.AsyncTask
16
+import android.text.Html.ImageGetter
8 17
 import android.view.LayoutInflater
9 18
 import android.view.ViewGroup
19
+import android.widget.TextView
10 20
 import androidx.constraintlayout.widget.ConstraintLayout
21
+import androidx.core.content.ContextCompat
11 22
 import androidx.core.text.HtmlCompat
12 23
 import androidx.core.widget.TextViewCompat
13 24
 import androidx.recyclerview.widget.RecyclerView
14 25
 import fr.forum_thalie.tsumugi.R
26
+import java.io.IOException
27
+import java.io.InputStream
28
+import java.net.URL
15 29
 import java.text.SimpleDateFormat
16 30
 import java.util.*
17
-import kotlin.collections.ArrayList
31
+
32
+// Using solution found here: https://stackoverflow.com/questions/3758535/display-images-on-android-using-textview-and-html-imagegetter-asynchronously
33
+// but without Picasso (just fetching image by myself.)
34
+class ImageGetterAsyncTask(
35
+    private val context: Context,
36
+    private val source: String,
37
+    private val levelListDrawable: LevelListDrawable
38
+) :
39
+    AsyncTask<TextView?, Void?, Bitmap?>() {
40
+    private var t: TextView? = null
41
+    override fun doInBackground(vararg params: TextView?): Bitmap? {
42
+        t = params[0]
43
+        return try {
44
+            //Log.d(LOG_CAT, "Downloading the image from: $source")
45
+            var k: InputStream? = null
46
+            var pic: Bitmap? = null
47
+            try {
48
+                k = URL(source).content as InputStream
49
+                val options = BitmapFactory.Options()
50
+                options.inSampleSize = 1/4
51
+                // this makes 1/2 of origin image size from width and height.
52
+                // it alleviates the memory for API16-API19 especially
53
+                pic = BitmapFactory.decodeStream(k, null, options)
54
+                k.close()
55
+            } catch (e: IOException) {
56
+                e.printStackTrace()
57
+            } finally {
58
+                k?.close()
59
+            }
60
+            pic
61
+        } catch (e: Exception) {
62
+            null
63
+        }
64
+    }
65
+
66
+    override fun onPostExecute(bitmap: Bitmap?) {
67
+        try {
68
+            val d: Drawable = BitmapDrawable(context.resources, bitmap)
69
+            val size = Point()
70
+            (context as Activity).windowManager.defaultDisplay.getSize(size)
71
+            // Lets calculate the ratio according to the screen width in px
72
+            val multiplier: Int = size.x / bitmap!!.width
73
+            //Log.d(LOG_CAT, "multiplier: $multiplier")
74
+            levelListDrawable.addLevel(1, 1, d)
75
+            // Set bounds width  and height according to the bitmap resized size
76
+            levelListDrawable.setBounds(
77
+                0,
78
+                0,
79
+                bitmap.width * multiplier,
80
+                bitmap.height * multiplier
81
+            )
82
+            levelListDrawable.level = 1
83
+            t!!.text = t!!.text // invalidate() doesn't work correctly...
84
+        } catch (e: Exception) { /* Like a null bitmap, etc. */
85
+        }
86
+    }
87
+
88
+}
18 89
 
19 90
 class NewsAdapter(private val dataSet: ArrayList<News>, private val c: Context
20 91
     /*,
@@ -56,12 +127,30 @@ class NewsAdapter(private val dataSet: ArrayList<News>, private val c: Context
56 127
             i.data = Uri.parse(dataSet[position].link)
57 128
             c.startActivity(i)
58 129
         }
59
-        text.text = HtmlCompat.fromHtml(dataSet[position].text, HtmlCompat.FROM_HTML_MODE_LEGACY)
60 130
         header.text = HtmlCompat.fromHtml(dataSet[position].header, HtmlCompat.FROM_HTML_MODE_LEGACY).replace(Regex("\n"), " ")
61 131
         author.text = "| ${dataSet[position].author}"
62 132
         val sdf = SimpleDateFormat("dd MMM yyyy", Locale.getDefault())
63 133
         date.text = sdf.format(dataSet[position].date)
64 134
         TextViewCompat.setAutoSizeTextTypeWithDefaults(author, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM)
135
+        //val html = HtmlCompat.fromHtml(dataSet[position].text, HtmlCompat.FROM_HTML_MODE_LEGACY)
136
+
137
+
138
+        val spanned = HtmlCompat.fromHtml(
139
+            dataSet[position].text,
140
+            HtmlCompat.FROM_HTML_MODE_LEGACY,
141
+            ImageGetter { source ->
142
+                val d = LevelListDrawable()
143
+                val empty: Drawable? = ContextCompat.getDrawable(c, R.drawable.exo_icon_circular_play)
144
+
145
+                d.addLevel(0, 0, empty!!)
146
+                d.setBounds(0, 0, empty.intrinsicWidth, empty.intrinsicHeight)
147
+                ImageGetterAsyncTask(c, source, d).execute(text)
148
+                d
149
+            }, null
150
+        )
151
+        text.text = spanned
152
+
153
+
65 154
     }
66 155
 
67 156
     // Return the size of your dataset (invoked by the layout manager)