From 3e06d787910a8c1b263197c92e225778562696a3 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 16 Dec 2018 18:02:13 +0100 Subject: [PATCH] show the image at the widget --- .../gallery/pro/activities/MainActivity.kt | 2 ++ .../gallery/pro/extensions/Context.kt | 13 ++++++++ .../gallery/pro/helpers/MyWidgetProvider.kt | 33 +++++++++++++++++-- app/src/main/res/layout/widget.xml | 3 +- app/src/main/res/layout/widget_config.xml | 7 ++-- app/src/main/res/values/dimens.xml | 1 + app/src/main/res/xml/widget_info.xml | 2 +- 7 files changed, 54 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt index 3af4e7043..f4345f291 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt @@ -145,6 +145,8 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { config.filterMedia += TYPE_SVGS } } + + updateWidgets() } override fun onStart() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index 161d131e7..17b0a2db9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -1,5 +1,7 @@ package com.simplemobiletools.gallery.pro.extensions +import android.appwidget.AppWidgetManager +import android.content.ComponentName import android.content.Context import android.content.Intent import android.content.res.Configuration @@ -487,3 +489,14 @@ fun Context.getUpdatedDeletedMedia(mediumDao: MediumDao): ArrayList { fun Context.deleteDBPath(mediumDao: MediumDao, path: String) { mediumDao.deleteMediumPath(path.replaceFirst(recycleBinPath, RECYCLE_BIN)) } + +fun Context.updateWidgets() { + val widgetIDs = AppWidgetManager.getInstance(applicationContext).getAppWidgetIds(ComponentName(applicationContext, MyWidgetProvider::class.java)) + if (widgetIDs.isNotEmpty()) { + Intent(applicationContext, MyWidgetProvider::class.java).apply { + action = AppWidgetManager.ACTION_APPWIDGET_UPDATE + putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIDs) + sendBroadcast(this) + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt index 08ef3081c..1affe0ec4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt @@ -3,13 +3,23 @@ package com.simplemobiletools.gallery.pro.helpers import android.app.PendingIntent import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetProvider +import android.content.ComponentName import android.content.Context import android.content.Intent +import android.os.Handler +import android.os.Looper +import android.util.Log import android.widget.RemoteViews +import com.bumptech.glide.Glide +import com.bumptech.glide.load.engine.DiskCacheStrategy +import com.bumptech.glide.request.RequestOptions +import com.bumptech.glide.request.target.AppWidgetTarget import com.simplemobiletools.commons.extensions.setBackgroundColor import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.activities.MediaActivity import com.simplemobiletools.gallery.pro.extensions.config +import com.simplemobiletools.gallery.pro.extensions.directoryDB +import com.simplemobiletools.gallery.pro.extensions.getFileSignature import com.simplemobiletools.gallery.pro.extensions.widgetsDB import com.simplemobiletools.gallery.pro.models.Widget @@ -29,8 +39,27 @@ class MyWidgetProvider : AppWidgetProvider() { context.widgetsDB.getWidgets().forEach { val views = RemoteViews(context.packageName, R.layout.widget) views.setBackgroundColor(R.id.widget_holder, context.config.widgetBgColor) - setupAppOpenIntent(context, views, R.id.widget_holder, it) - appWidgetManager.updateAppWidget(it.widgetId, views) + + val path = context.directoryDB.getDirectoryThumbnail(it.folderPath) + val options = RequestOptions() + .signature(path!!.getFileSignature()) + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + if (context.config.cropThumbnails) options.centerCrop() else options.fitCenter() + + Handler(Looper.getMainLooper()).post { + val widgetSize = context.resources.getDimension(R.dimen.widget_initial_width).toInt() + val componentName = ComponentName(context, MyWidgetProvider::class.java) + val appWidgetTarget = object : AppWidgetTarget(context, widgetSize, widgetSize, R.id.widget_imageview, views, componentName) {} + + Glide.with(context) + .asBitmap() + .load(path) + .apply(options) + .into(appWidgetTarget) + + setupAppOpenIntent(context, views, R.id.widget_holder, it) + appWidgetManager.updateAppWidget(it.widgetId, views) + } } }.start() } diff --git a/app/src/main/res/layout/widget.xml b/app/src/main/res/layout/widget.xml index 2be9e64d5..825e4ab54 100644 --- a/app/src/main/res/layout/widget.xml +++ b/app/src/main/res/layout/widget.xml @@ -1,6 +1,7 @@ @@ -9,6 +10,6 @@ android:id="@+id/widget_imageview" android:layout_width="match_parent" android:layout_height="wrap_content" - android:src="@mipmap/ic_launcher"/> + tools:src="@mipmap/ic_launcher"/> diff --git a/app/src/main/res/layout/widget_config.xml b/app/src/main/res/layout/widget_config.xml index d8b56d5d1..bc9624e00 100644 --- a/app/src/main/res/layout/widget_config.xml +++ b/app/src/main/res/layout/widget_config.xml @@ -33,9 +33,10 @@ + android:layout_width="134dp" + android:layout_height="134dp" + android:layout_below="@+id/folder_picker_holder" + android:padding="@dimen/normal_margin"> 90dp 180dp 86dp + 110dp diff --git a/app/src/main/res/xml/widget_info.xml b/app/src/main/res/xml/widget_info.xml index ecfa6d765..ad8f3ab5f 100644 --- a/app/src/main/res/xml/widget_info.xml +++ b/app/src/main/res/xml/widget_info.xml @@ -3,7 +3,7 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:configure="com.simplemobiletools.gallery.pro.activities.WidgetConfigureActivity" android:initialLayout="@layout/widget" - android:minWidth="40dp" + android:minWidth="@dimen/widget_initial_width" android:minHeight="40dp" android:minResizeWidth="40dp" android:minResizeHeight="40dp"