fix some threading issues related to widgets

This commit is contained in:
tibbi 2018-12-16 20:55:41 +01:00
parent 7fc10d74d4
commit 069d991fac

View file

@ -3,16 +3,12 @@ package com.simplemobiletools.gallery.pro.helpers
import android.app.PendingIntent import android.app.PendingIntent
import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.widget.RemoteViews import android.widget.RemoteViews
import com.bumptech.glide.Glide import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.AppWidgetTarget
import com.simplemobiletools.commons.extensions.setBackgroundColor import com.simplemobiletools.commons.extensions.setBackgroundColor
import com.simplemobiletools.commons.extensions.setText import com.simplemobiletools.commons.extensions.setText
import com.simplemobiletools.commons.extensions.setVisibleIf import com.simplemobiletools.commons.extensions.setVisibleIf
@ -34,11 +30,13 @@ class MyWidgetProvider : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
super.onUpdate(context, appWidgetManager, appWidgetIds) super.onUpdate(context, appWidgetManager, appWidgetIds)
Thread { Thread {
context.widgetsDB.getWidgets().forEach { val widgets = context.widgetsDB.getWidgets()
val config = context.config
widgets.forEach {
val views = RemoteViews(context.packageName, R.layout.widget).apply { val views = RemoteViews(context.packageName, R.layout.widget).apply {
setBackgroundColor(R.id.widget_holder, context.config.widgetBgColor) setBackgroundColor(R.id.widget_holder, config.widgetBgColor)
setVisibleIf(R.id.widget_folder_name, context.config.showWidgetFolderName) setVisibleIf(R.id.widget_folder_name, config.showWidgetFolderName)
setTextColor(R.id.widget_folder_name, context.config.widgetTextColor) setTextColor(R.id.widget_folder_name, config.widgetTextColor)
setText(R.id.widget_folder_name, context.getFolderNameFromPath(it.folderPath)) setText(R.id.widget_folder_name, context.getFolderNameFromPath(it.folderPath))
} }
@ -48,21 +46,19 @@ class MyWidgetProvider : AppWidgetProvider() {
.diskCacheStrategy(DiskCacheStrategy.RESOURCE) .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
if (context.config.cropThumbnails) options.centerCrop() else options.fitCenter() if (context.config.cropThumbnails) options.centerCrop() else options.fitCenter()
Handler(Looper.getMainLooper()).post {
val widgetSize = context.resources.getDimension(R.dimen.widget_initial_size).toInt() val widgetSize = context.resources.getDimension(R.dimen.widget_initial_size).toInt()
val componentName = ComponentName(context, MyWidgetProvider::class.java) val image = Glide.with(context)
val appWidgetTarget = object : AppWidgetTarget(context, widgetSize, widgetSize, R.id.widget_imageview, views, componentName) {}
Glide.with(context)
.asBitmap() .asBitmap()
.load(path) .load(path)
.apply(options) .apply(options)
.into(appWidgetTarget) .submit(widgetSize, widgetSize)
.get()
views.setImageViewBitmap(R.id.widget_imageview, image)
setupAppOpenIntent(context, views, R.id.widget_holder, it) setupAppOpenIntent(context, views, R.id.widget_holder, it)
appWidgetManager.updateAppWidget(it.widgetId, views) appWidgetManager.updateAppWidget(it.widgetId, views)
} }
}
}.start() }.start()
} }
} }