Skip to content

Commit 5e2a3d3

Browse files
Backport dynamic layout for search widget
1 parent 44defe6 commit 5e2a3d3

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ dependencies {
185185
implementation libs.material
186186
implementation libs.appcompat
187187
implementation libs.core.ktx
188+
implementation libs.androidx.core.remoteviews
188189
implementation libs.browser
189190
implementation libs.constraintlayout
190191
implementation libs.fragment.ktx

app/src/main/java/org/wikipedia/widgets/WidgetProviderSearch.kt

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,44 +7,52 @@ import android.content.ComponentName
77
import android.content.Context
88
import android.content.Intent
99
import android.os.Build
10-
import android.util.SizeF
10+
import android.os.Bundle
1111
import android.widget.RemoteViews
1212
import androidx.core.app.PendingIntentCompat
13+
import androidx.core.util.SizeFCompat
14+
import androidx.core.widget.updateAppWidget
1315
import org.wikipedia.Constants.InvokeSource
1416
import org.wikipedia.R
1517
import org.wikipedia.search.SearchActivity
1618

1719
class 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
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ coilCompose = "3.3.0"
88
commonsLang3 = "3.18.0"
99
constraintlayout = "2.2.1"
1010
coreKtx = "1.17.0"
11+
coreRemoteviews = "1.1.0"
1112
desugar_jdk_libs = "2.1.5"
1213
drawerlayout = "1.2.0"
1314
espressoVersion = "3.7.0"
@@ -55,6 +56,7 @@ composeViewModel = "2.9.3"
5556
[libraries]
5657
android-sdk = { module = "org.maplibre.gl:android-sdk", version.ref = "androidSdk" }
5758
android-plugin-annotation-v9 = { module = "org.maplibre.gl:android-plugin-annotation-v9", version.ref = "androidPluginAnnotationV9" }
59+
androidx-core-remoteviews = { module = "androidx.core:core-remoteviews", version.ref = "coreRemoteviews" }
5860
androidx-espresso-intents = { module = "androidx.test.espresso:espresso-intents", version.ref = "espressoVersion" }
5961
androidx-junit = { module = "androidx.test.ext:junit", version.ref = "junitVersion" }
6062
androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigationCompose" }

0 commit comments

Comments
 (0)