1+ package com.wstxda.switchai.widget
2+
3+ import android.app.PendingIntent
4+ import android.appwidget.AppWidgetManager
5+ import android.appwidget.AppWidgetProvider
6+ import android.content.Context
7+ import android.content.Intent
8+ import android.os.Bundle
9+ import android.widget.RemoteViews
10+ import com.wstxda.switchai.R
11+ import com.wstxda.switchai.services.DigitalAssistantFallbackService
12+
13+ class AssistantInvisibleWidgetProvider : AppWidgetProvider () {
14+
15+ override fun onUpdate (
16+ context : Context , appWidgetManager : AppWidgetManager , appWidgetIds : IntArray ,
17+ ) {
18+ for (appWidgetId in appWidgetIds) {
19+ updateWidget(context, appWidgetManager, appWidgetId)
20+ }
21+ }
22+
23+ override fun onAppWidgetOptionsChanged (
24+ context : Context , appWidgetManager : AppWidgetManager , appWidgetId : Int , newOptions : Bundle ,
25+ ) {
26+ updateWidget(context, appWidgetManager, appWidgetId)
27+ }
28+
29+ private fun updateWidget (
30+ context : Context , appWidgetManager : AppWidgetManager , appWidgetId : Int ,
31+ ) {
32+ val options = appWidgetManager.getAppWidgetOptions(appWidgetId)
33+ val minWidth = options.getInt(AppWidgetManager .OPTION_APPWIDGET_MIN_WIDTH )
34+
35+ val layoutId = if (minWidth < 150 ) {
36+ R .layout.widget_assistant_invisible_small
37+ } else {
38+ R .layout.widget_assistant_invisible_default
39+ }
40+
41+ val views = RemoteViews (context.packageName, layoutId)
42+
43+ val intent = Intent (context, DigitalAssistantFallbackService ::class .java).apply {
44+ flags = Intent .FLAG_ACTIVITY_NEW_TASK
45+ }
46+
47+ val pendingIntent = PendingIntent .getActivity(
48+ context,
49+ appWidgetId,
50+ intent,
51+ PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
52+ )
53+
54+ views.setOnClickPendingIntent(R .id.widget_assistant_invisible, pendingIntent)
55+
56+ appWidgetManager.updateAppWidget(appWidgetId, views)
57+ }
58+ }
0 commit comments