@@ -7,44 +7,52 @@ import android.content.ComponentName
77import android.content.Context
88import android.content.Intent
99import android.os.Build
10- import android.util.SizeF
10+ import android.os.Bundle
1111import android.widget.RemoteViews
1212import androidx.core.app.PendingIntentCompat
13+ import androidx.core.util.SizeFCompat
14+ import androidx.core.widget.updateAppWidget
1315import org.wikipedia.Constants.InvokeSource
1416import org.wikipedia.R
1517import org.wikipedia.search.SearchActivity
1618
1719class WidgetProviderSearch : AppWidgetProvider () {
1820 override fun onUpdate (context : Context , appWidgetManager : AppWidgetManager , appWidgetIds : IntArray ) {
1921 val thisWidget = ComponentName (context, WidgetProviderSearch ::class .java)
20- val allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget)
21- for (widgetId in allWidgetIds) {
22-
23- val pendingIntent = PendingIntentCompat .getActivity(context, 0 ,
24- SearchActivity .newIntent(context, InvokeSource .WIDGET , null )
25- .setFlags(Intent .FLAG_ACTIVITY_NEW_TASK ),
26- PendingIntent .FLAG_UPDATE_CURRENT , false )
22+ for (widgetId in appWidgetManager.getAppWidgetIds(thisWidget)) {
23+ updateWidget(context, appWidgetManager, widgetId)
24+ }
25+ }
2726
28- val largeView = RemoteViews (context.packageName, R .layout.widget_search_large)
29- largeView.setOnClickPendingIntent(R .id.widget_container, pendingIntent)
27+ override fun onAppWidgetOptionsChanged (
28+ context : Context ,
29+ appWidgetManager : AppWidgetManager ,
30+ appWidgetId : Int ,
31+ newOptions : Bundle ,
32+ ) {
33+ // appWidgetManager.updateAppWidget must be called here on API levels < 31
34+ if (Build .VERSION .SDK_INT < Build .VERSION_CODES .S ) {
35+ updateWidget(context, appWidgetManager, appWidgetId)
36+ }
37+ }
3038
31- val remoteViews = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
32- val smallView = RemoteViews (context.packageName, R .layout.widget_search_small)
33- val mediumView = RemoteViews (context.packageName, R .layout.widget_search_medium)
34- smallView.setOnClickPendingIntent(R .id.widget_container, pendingIntent)
35- mediumView.setOnClickPendingIntent(R .id.widget_container, pendingIntent)
39+ private fun updateWidget (context : Context , appWidgetManager : AppWidgetManager , appWidgetId : Int ) {
40+ val small = SizeFCompat (32f , 32f )
41+ val medium = SizeFCompat (64f , 32f )
42+ val large = SizeFCompat (160f , 32f )
43+ val pendingIntent = PendingIntentCompat .getActivity(context, 0 ,
44+ SearchActivity .newIntent(context, InvokeSource .WIDGET , null )
45+ .setFlags(Intent .FLAG_ACTIVITY_NEW_TASK ),
46+ PendingIntent .FLAG_UPDATE_CURRENT , false )
3647
37- val viewMapping: Map <SizeF , RemoteViews > = mapOf (
38- SizeF (32f , 32f ) to smallView,
39- SizeF (64f , 32f ) to mediumView,
40- SizeF (160f , 32f ) to largeView
41- )
42- RemoteViews (viewMapping)
43- } else {
44- largeView
48+ appWidgetManager.updateAppWidget(appWidgetId, listOf (small, medium, large)) {
49+ val remoteViewId = when (it) {
50+ small -> R .layout.widget_search_small
51+ medium -> R .layout.widget_search_medium
52+ else -> R .layout.widget_search_large
4553 }
46-
47- appWidgetManager.updateAppWidget(widgetId, remoteViews)
54+ RemoteViews (context.packageName, remoteViewId)
55+ . apply { setOnClickPendingIntent( R .id.widget_container, pendingIntent) }
4856 }
4957 }
5058}
0 commit comments