2525
2626package org.toni.customfetch_android.widget
2727
28+ import android.app.PendingIntent
2829import android.appwidget.AppWidgetManager
2930import android.appwidget.AppWidgetProvider
3031import android.content.Context
32+ import android.content.Intent
3133import android.content.res.Configuration.ORIENTATION_PORTRAIT
3234import android.os.Bundle
3335import android.text.TextPaint
3436import android.util.Log
3537import android.widget.RemoteViews
3638import org.toni.customfetch_android.R
3739
40+ const val WIDGET_CLICK_ACTION = " org.toni.customfetch_android.WIDGET_CLICK"
41+
3842/* *
3943 * Implementation of App Widget functionality.
4044 * App Widget Configuration implemented in [customfetchConfigureActivity]
@@ -46,8 +50,9 @@ class customfetch : AppWidgetProvider() {
4650 appWidgetIds : IntArray
4751 ) {
4852 // There may be multiple widgets active, so update all of them
49- for (appWidgetId in appWidgetIds)
53+ for (appWidgetId in appWidgetIds) {
5054 updateAppWidget(context, appWidgetManager, appWidgetId)
55+ }
5156 }
5257
5358 override fun onDeleted (context : Context , appWidgetIds : IntArray ) {
@@ -57,6 +62,22 @@ class customfetch : AppWidgetProvider() {
5762 }
5863 }
5964
65+ override fun onReceive (context : Context ? , intent : Intent ? ) {
66+ super .onReceive(context, intent)
67+ Log .d(" onReceiveTest" , " intent.action = ${intent?.action} " )
68+ if (context != null && intent?.action == WIDGET_CLICK_ACTION ) {
69+ val appWidgetId = intent.getIntExtra(
70+ AppWidgetManager .EXTRA_APPWIDGET_ID ,
71+ AppWidgetManager .INVALID_APPWIDGET_ID
72+ )
73+
74+ if (appWidgetId != AppWidgetManager .INVALID_APPWIDGET_ID ) {
75+ Log .d(" onReceiveTest" , " Widget clicked!" )
76+ updateAppWidget(context, AppWidgetManager .getInstance(context), appWidgetId)
77+ }
78+ }
79+ }
80+
6081 override fun onAppWidgetOptionsChanged (
6182 context : Context ,
6283 appWidgetManager : AppWidgetManager ,
@@ -65,15 +86,6 @@ class customfetch : AppWidgetProvider() {
6586 ) {
6687 updateAppWidget(context, appWidgetManager, appWidgetId)
6788 }
68-
69- override fun onEnabled (context : Context ) {
70- // Enter relevant functionality for when the first widget is created
71-
72- }
73-
74- override fun onDisabled (context : Context ) {
75- // Enter relevant functionality for when the last widget is disabled
76- }
7789}
7890
7991// https://stackoverflow.com/a/58501760
@@ -117,7 +129,6 @@ internal fun updateAppWidget(
117129 context : Context ,
118130 appWidgetManager : AppWidgetManager ,
119131 appWidgetId : Int
120-
121132) {
122133 val disableLineWrap = getDisableLineWrap(context, appWidgetId)
123134 val bgColor = getBgColor(context, appWidgetId)
@@ -145,8 +156,22 @@ internal fun updateAppWidget(
145156 textPaint
146157 )
147158
159+ // needed for when touching the widget
160+ val intent = Intent (context, customfetch::class .java).apply {
161+ action = WIDGET_CLICK_ACTION
162+ putExtra(AppWidgetManager .EXTRA_APPWIDGET_ID , appWidgetId)
163+ }
164+
165+ val pendingIntent = PendingIntent .getBroadcast(
166+ context,
167+ appWidgetId,
168+ intent,
169+ PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
170+ )
171+
148172 // Construct the RemoteViews object
149173 val views = RemoteViews (context.packageName, R .layout.customfetch)
174+ views.setOnClickPendingIntent(R .id.widget_root, pendingIntent)
150175 views.setTextViewText(R .id.customfetch_text, parsedContent)
151176 views.setInt(R .id.widget_root, " setBackgroundColor" , bgColor);
152177
0 commit comments