Fix website icon loading on main thread
This commit is contained in:
parent
fa22eb4d71
commit
2273a0912a
@ -6,7 +6,6 @@ import android.graphics.Color
|
||||
import android.graphics.Typeface
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import android.net.Uri
|
||||
import android.util.Log
|
||||
import android.webkit.URLUtil
|
||||
import androidx.core.graphics.drawable.toBitmap
|
||||
import androidx.core.graphics.toColorInt
|
||||
@ -15,15 +14,15 @@ import com.bumptech.glide.Glide
|
||||
import de.mm20.launcher2.graphics.TextDrawable
|
||||
import de.mm20.launcher2.helper.NetworkUtils
|
||||
import de.mm20.launcher2.icons.LauncherIcon
|
||||
import de.mm20.launcher2.ktx.jsonObjectOf
|
||||
import de.mm20.launcher2.ktx.sp
|
||||
import de.mm20.launcher2.preferences.LauncherPreferences
|
||||
import de.mm20.launcher2.preferences.WebsiteProtocols
|
||||
import de.mm20.launcher2.websites.R
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import org.json.JSONObject
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.UncheckedIOException
|
||||
import java.io.IOException
|
||||
@ -45,6 +44,7 @@ class Website(
|
||||
override suspend fun loadIconAsync(context: Context, size: Int): LauncherIcon? {
|
||||
if (favicon.isEmpty()) return null
|
||||
try {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val icon = Glide.with(context)
|
||||
.asDrawable()
|
||||
.load(favicon)
|
||||
@ -56,11 +56,12 @@ class Website(
|
||||
.generate()
|
||||
.getLightMutedColor(Color.WHITE)
|
||||
}
|
||||
return LauncherIcon(
|
||||
LauncherIcon(
|
||||
foreground = icon,
|
||||
background = ColorDrawable(color),
|
||||
foregroundScale = 0.7f
|
||||
)
|
||||
}
|
||||
} catch (e: ExecutionException) {
|
||||
return null
|
||||
}
|
||||
@ -69,7 +70,11 @@ class Website(
|
||||
|
||||
override fun getPlaceholderIcon(context: Context): LauncherIcon {
|
||||
val drawable = if (label.isNotEmpty()) {
|
||||
TextDrawable(label[0].toString(), typeface = Typeface.DEFAULT_BOLD, fontSize = 40 * context.sp)
|
||||
TextDrawable(
|
||||
label[0].toString(),
|
||||
typeface = Typeface.DEFAULT_BOLD,
|
||||
fontSize = 40 * context.sp
|
||||
)
|
||||
} else context.getDrawable(R.drawable.ic_website)!!
|
||||
return LauncherIcon(
|
||||
foreground = drawable,
|
||||
@ -90,9 +95,12 @@ class Website(
|
||||
var url = query
|
||||
val prefs = LauncherPreferences.instance
|
||||
if (!prefs.searchWebsite ||
|
||||
NetworkUtils.isOffline(context, prefs.searchWebsitesMobileData)) return null
|
||||
val protocol = if (LauncherPreferences.instance.searchWebsitesProtocol == WebsiteProtocols.HTTPS) "https://" else "http://"
|
||||
if (!query.startsWith("https://") && !query.startsWith("http://")) url = "$protocol$query"
|
||||
NetworkUtils.isOffline(context, prefs.searchWebsitesMobileData)
|
||||
) return null
|
||||
val protocol =
|
||||
if (LauncherPreferences.instance.searchWebsitesProtocol == WebsiteProtocols.HTTPS) "https://" else "http://"
|
||||
if (!query.startsWith("https://") && !query.startsWith("http://")) url =
|
||||
"$protocol$query"
|
||||
if (!URLUtil.isValidUrl(url)) return null
|
||||
try {
|
||||
val request = Request.Builder()
|
||||
@ -108,7 +116,8 @@ class Website(
|
||||
if (title.isBlank()) title = doc.title()
|
||||
if (title.isBlank()) title = url
|
||||
var description = doc.select("meta[property=og:description]").attr("content")
|
||||
if (description.isBlank()) description = doc.select("meta[name=description]").attr("content")
|
||||
if (description.isBlank()) description =
|
||||
doc.select("meta[name=description]").attr("content")
|
||||
val color = try {
|
||||
val colorString = doc.select("meta[name=theme-color]").attr("content")
|
||||
if (colorString.isNotEmpty()) colorString.toColorInt()
|
||||
@ -118,9 +127,11 @@ class Website(
|
||||
}
|
||||
var image = doc.select("meta[property=og:image]").attr("content")
|
||||
var favicon = doc.select("link[rel=apple-touch-icon]").attr("href")
|
||||
if (favicon.isBlank()) favicon = doc.head().select("meta[itemprop=image]").attr("content")
|
||||
if (favicon.isBlank()) favicon =
|
||||
doc.head().select("meta[itemprop=image]").attr("content")
|
||||
if (favicon.isBlank()) favicon = doc.select("link[rel=icon]").attr("href")
|
||||
if (favicon.isBlank()) favicon = doc.head().select("link[href~=.*\\.(ico|png)]").attr("href")
|
||||
if (favicon.isBlank()) favicon =
|
||||
doc.head().select("link[href~=.*\\.(ico|png)]").attr("href")
|
||||
if (!favicon.isBlank()) favicon = resolve(response.request.url, favicon)
|
||||
if (!image.isBlank()) image = resolve(response.request.url, image)
|
||||
return Website(
|
||||
@ -129,7 +140,8 @@ class Website(
|
||||
description = description,
|
||||
image = image,
|
||||
favicon = favicon,
|
||||
color = color)
|
||||
color = color
|
||||
)
|
||||
} catch (e: IOException) {
|
||||
//Ignore. Not a HTML page or no connection. No result for this query
|
||||
} catch (e: UncheckedIOException) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user