Skip to content

Commit 7b532fd

Browse files
committed
fix: getLaunchIntent ANR
Signed-off-by: Hu Shenghao <dede.hu@qq.com>
1 parent 989c072 commit 7b532fd

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

feature/widget/src/main/java/com/dede/android_eggs/views/widget/AnalogClockAppWidget.kt

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import android.widget.RemoteViews
1010
import androidx.core.app.PendingIntentCompat
1111
import com.dede.basic.Utils
1212
import com.dede.basic.cachedExecutor
13+
import kotlinx.coroutines.Dispatchers
14+
import kotlinx.coroutines.runBlocking
15+
import kotlinx.coroutines.withTimeoutOrNull
1316

1417
/**
1518
* Easter Eggs Analog clock widget.
@@ -22,7 +25,7 @@ class AnalogClockAppWidget : AppWidgetProvider() {
2225
appWidgetIds: IntArray,
2326
) {
2427
for (appWidgetId in appWidgetIds) {
25-
updateAppWidget(
28+
updateAppWidgetAsync(
2629
context,
2730
appWidgetManager,
2831
appWidgetId
@@ -36,33 +39,37 @@ class AnalogClockAppWidget : AppWidgetProvider() {
3639
appWidgetId: Int,
3740
newOptions: Bundle?,
3841
) {
39-
updateAppWidget(context, appWidgetManager, appWidgetId)
42+
updateAppWidgetAsync(context, appWidgetManager, appWidgetId)
4043
}
4144

4245
}
4346

4447
private const val EXTRA_FROM_WIDGET = "extra_from_widget"
4548

46-
private fun updateAppWidget(
49+
private fun updateAppWidgetAsync(
4750
context: Context,
4851
appWidgetManager: AppWidgetManager,
4952
appWidgetId: Int,
5053
) {
51-
val views = RemoteViews(context.packageName, R.layout.widget_easter_egg_analog_clock)
52-
53-
val launchIntent: Intent? = Utils.getLaunchIntent(context)
54-
if (launchIntent != null) {
55-
val intent = PendingIntentCompat.getActivity(
56-
context, 0,
57-
launchIntent.putExtra(EXTRA_FROM_WIDGET, appWidgetId),
58-
PendingIntent.FLAG_UPDATE_CURRENT,
59-
false
60-
)
61-
views.setOnClickPendingIntent(R.id.analog_clock, intent)
62-
}
63-
6454
cachedExecutor.execute {
65-
// Binder call
66-
appWidgetManager.updateAppWidget(appWidgetId, views)
55+
runBlocking(Dispatchers.IO) {
56+
val views = RemoteViews(context.packageName, R.layout.widget_easter_egg_analog_clock)
57+
58+
val launchIntent: Intent? = withTimeoutOrNull(300) {
59+
// binder call
60+
Utils.getLaunchIntent(context)
61+
}
62+
if (launchIntent != null) {
63+
val intent = PendingIntentCompat.getActivity(
64+
context, 0,
65+
launchIntent.putExtra(EXTRA_FROM_WIDGET, appWidgetId),
66+
PendingIntent.FLAG_UPDATE_CURRENT,
67+
false
68+
)
69+
views.setOnClickPendingIntent(R.id.analog_clock, intent)
70+
}
71+
// binder call
72+
appWidgetManager.updateAppWidget(appWidgetId, views)
73+
}
6774
}
6875
}

0 commit comments

Comments
 (0)