Skip to content

Commit

Permalink
fix: getLaunchIntent ANR
Browse files Browse the repository at this point in the history
Signed-off-by: Hu Shenghao <[email protected]>
  • Loading branch information
hushenghao committed Dec 11, 2024
1 parent 989c072 commit 7b532fd
Showing 1 changed file with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import android.widget.RemoteViews
import androidx.core.app.PendingIntentCompat
import com.dede.basic.Utils
import com.dede.basic.cachedExecutor
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeoutOrNull

/**
* Easter Eggs Analog clock widget.
Expand All @@ -22,7 +25,7 @@ class AnalogClockAppWidget : AppWidgetProvider() {
appWidgetIds: IntArray,
) {
for (appWidgetId in appWidgetIds) {
updateAppWidget(
updateAppWidgetAsync(
context,
appWidgetManager,
appWidgetId
Expand All @@ -36,33 +39,37 @@ class AnalogClockAppWidget : AppWidgetProvider() {
appWidgetId: Int,
newOptions: Bundle?,
) {
updateAppWidget(context, appWidgetManager, appWidgetId)
updateAppWidgetAsync(context, appWidgetManager, appWidgetId)
}

}

private const val EXTRA_FROM_WIDGET = "extra_from_widget"

private fun updateAppWidget(
private fun updateAppWidgetAsync(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetId: Int,
) {
val views = RemoteViews(context.packageName, R.layout.widget_easter_egg_analog_clock)

val launchIntent: Intent? = Utils.getLaunchIntent(context)
if (launchIntent != null) {
val intent = PendingIntentCompat.getActivity(
context, 0,
launchIntent.putExtra(EXTRA_FROM_WIDGET, appWidgetId),
PendingIntent.FLAG_UPDATE_CURRENT,
false
)
views.setOnClickPendingIntent(R.id.analog_clock, intent)
}

cachedExecutor.execute {
// Binder call
appWidgetManager.updateAppWidget(appWidgetId, views)
runBlocking(Dispatchers.IO) {
val views = RemoteViews(context.packageName, R.layout.widget_easter_egg_analog_clock)

val launchIntent: Intent? = withTimeoutOrNull(300) {
// binder call
Utils.getLaunchIntent(context)
}
if (launchIntent != null) {
val intent = PendingIntentCompat.getActivity(
context, 0,
launchIntent.putExtra(EXTRA_FROM_WIDGET, appWidgetId),
PendingIntent.FLAG_UPDATE_CURRENT,
false
)
views.setOnClickPendingIntent(R.id.analog_clock, intent)
}
// binder call
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}
}

0 comments on commit 7b532fd

Please sign in to comment.