Skip to content

Commit 1be548d

Browse files
committed
Added assistant widgets
- Material3 style widget - Invisible widget
1 parent a2664fd commit 1be548d

20 files changed

+349
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,39 @@
345345
</intent-filter>
346346
</activity>
347347

348+
<!--assistant widget-->
349+
350+
<receiver
351+
android:name=".widget.AssistantWidgetProvider"
352+
android:label="@string/widget_assistant_label"
353+
android:exported="true">
354+
<intent-filter>
355+
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
356+
</intent-filter>
357+
<meta-data
358+
android:name="android.appwidget.provider"
359+
android:resource="@xml/widget_assistant_info" />
360+
</receiver>
361+
362+
<!--assistant invisible widget-->
363+
364+
<receiver
365+
android:name=".widget.AssistantInvisibleWidgetProvider"
366+
android:exported="true"
367+
android:label="@string/widget_assistant_invisible_label">
368+
<intent-filter>
369+
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
370+
</intent-filter>
371+
<meta-data
372+
android:name="android.appwidget.provider"
373+
android:resource="@xml/widget_assistant_invisible_info" />
374+
</receiver>
375+
376+
<!--widget configure activity-->
377+
<activity
378+
android:name=".widget.WidgetConfigureActivity"
379+
android:exported="true" />
380+
348381
<!--digital assistant quick settings tile service-->
349382

350383
<service
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 AssistantWidgetProvider : 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_small
37+
} else {
38+
R.layout.widget_assistant_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, pendingIntent)
55+
56+
appWidgetManager.updateAppWidget(appWidgetId, views)
57+
}
58+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.wstxda.switchai.widget
2+
3+
import android.app.Activity
4+
import android.appwidget.AppWidgetManager
5+
import android.content.Intent
6+
import android.os.Bundle
7+
import com.wstxda.switchai.activity.MainActivity
8+
9+
class WidgetConfigureActivity : Activity() {
10+
11+
private var appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID
12+
13+
override fun onCreate(savedInstanceState: Bundle?) {
14+
super.onCreate(savedInstanceState)
15+
16+
appWidgetId = intent?.extras?.getInt(
17+
AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID
18+
) ?: AppWidgetManager.INVALID_APPWIDGET_ID
19+
20+
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
21+
val resultValue = Intent().apply {
22+
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
23+
}
24+
setResult(RESULT_OK, resultValue)
25+
startActivity(Intent(this, MainActivity::class.java))
26+
27+
finish()
28+
} else {
29+
finish()
30+
}
31+
}
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:width="18dp"
4+
android:height="18dp"
5+
android:viewportWidth="18"
6+
android:viewportHeight="18">
7+
<path
8+
android:fillColor="#6FFFFFFF"
9+
android:pathData="M1.88 5.25c0 0.44 0.08 0.88 0.25 1.3 0.17 0.4 0.42 0.77 0.73 1.09 0.32 0.31 0.69 0.56 1.1 0.73 0.4 0.17 0.85 0.26 1.29 0.26 0.44 0 0.88-0.1 1.3-0.26 0.4-0.17 0.77-0.42 1.09-0.73 0.31-0.32 0.56-0.69 0.73-1.1 0.17-0.4 0.26-0.85 0.26-1.29 0-0.9-0.36-1.75-1-2.39C7 2.23 6.16 1.88 5.26 1.88c-0.9 0-1.75 0.35-2.39 0.98-0.63 0.64-0.98 1.5-0.98 2.39Zm0 7.5c0 0.44 0.08 0.88 0.25 1.3 0.17 0.4 0.42 0.77 0.73 1.09 0.32 0.31 0.69 0.56 1.1 0.73 0.4 0.17 0.85 0.26 1.29 0.26 0.44 0 0.88-0.1 1.3-0.26 0.4-0.17 0.77-0.42 1.09-0.73 0.31-0.32 0.56-0.69 0.73-1.1 0.17-0.4 0.26-0.85 0.26-1.29 0-0.9-0.36-1.75-1-2.39C7 9.73 6.16 9.38 5.26 9.38c-0.9 0-1.75 0.35-2.39 0.98-0.63 0.64-0.98 1.5-0.98 2.39Zm7.5 0c0 0.9 0.35 1.75 0.98 2.39 0.64 0.63 1.5 0.98 2.39 0.98 0.9 0 1.75-0.35 2.39-0.98 0.63-0.64 0.98-1.5 0.98-2.39 0-0.9-0.35-1.75-0.98-2.39-0.64-0.63-1.5-0.98-2.39-0.98-0.9 0-1.75 0.35-2.39 0.98-0.63 0.64-0.98 1.5-0.98 2.39Zm-2.26-7.5c0 0.5-0.2 0.97-0.54 1.33-0.36 0.35-0.83 0.54-1.33 0.54-0.5 0-0.97-0.2-1.33-0.54-0.35-0.36-0.54-0.83-0.54-1.33 0-0.5 0.2-0.97 0.54-1.33 0.36-0.35 0.83-0.54 1.33-0.54 0.5 0 0.97 0.2 1.33 0.54 0.35 0.36 0.54 0.83 0.54 1.33Zm0 7.5c0 0.5-0.2 0.97-0.54 1.33-0.36 0.35-0.83 0.54-1.33 0.54-0.5 0-0.97-0.2-1.33-0.54-0.35-0.36-0.54-0.83-0.54-1.33 0-0.5 0.2-0.97 0.54-1.33 0.36-0.35 0.83-0.54 1.33-0.54 0.5 0 0.97 0.2 1.33 0.54 0.35 0.36 0.54 0.83 0.54 1.33Zm7.5 0c0 0.5-0.2 0.97-0.54 1.33-0.36 0.35-0.83 0.54-1.33 0.54-0.5 0-0.97-0.2-1.33-0.54-0.35-0.36-0.54-0.83-0.54-1.33 0-0.5 0.2-0.97 0.54-1.33 0.36-0.35 0.83-0.54 1.33-0.54 0.5 0 0.97 0.2 1.33 0.54 0.35 0.36 0.54 0.83 0.54 1.33Zm-1.48-4.38l0.21-0.48c0.37-0.85 1.03-1.52 1.87-1.9l0.64-0.29c0.35-0.15 0.35-0.65 0-0.8l-0.6-0.28c-0.86-0.38-1.54-1.09-1.9-1.96l-0.21-0.52C13.1 2.07 13.06 2 12.99 1.94c-0.07-0.04-0.15-0.06-0.24-0.06s-0.17 0.02-0.24 0.07c-0.07 0.05-0.13 0.12-0.16 0.2l-0.21 0.51c-0.36 0.87-1.04 1.58-1.9 1.96L9.64 4.9c-0.35 0.16-0.35 0.66 0 0.81L10.28 6c0.84 0.38 1.5 1.05 1.87 1.9l0.2 0.48c0.04 0.07 0.1 0.14 0.16 0.19 0.07 0.04 0.16 0.07 0.24 0.07 0.08 0 0.17-0.03 0.24-0.07 0.07-0.05 0.12-0.12 0.15-0.2Z"
10+
tools:ignore="VectorPath" />
11+
</vector>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<shape xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:shape="rectangle">
3+
<solid android:color="@color/background_color" />
4+
<corners android:radius="16dp" />
5+
</shape>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:id="@+id/widget_assistant"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:background="@drawable/widget_background"
7+
android:gravity="center"
8+
android:orientation="vertical">
9+
10+
<ImageView
11+
android:layout_width="30dp"
12+
android:layout_height="30dp"
13+
android:contentDescription="@string/assistant_open"
14+
android:src="@drawable/ic_assistant"
15+
android:tint="@color/icon_color"
16+
tools:ignore="UseAppTint" />
17+
18+
<TextView
19+
android:layout_width="wrap_content"
20+
android:layout_height="wrap_content"
21+
android:layout_marginTop="8dp"
22+
android:text="@string/assistant_open"
23+
android:textColor="@color/text_color"
24+
android:textSize="16sp"
25+
android:textStyle="bold" />
26+
</LinearLayout>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:id="@+id/widget_assistant_invisible"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:background="@android:color/transparent">
6+
7+
<ImageView
8+
android:layout_width="wrap_content"
9+
android:layout_height="wrap_content"
10+
android:layout_gravity="bottom|end"
11+
android:layout_margin="16dp"
12+
android:contentDescription="@string/assistant_open"
13+
android:src="@drawable/ic_widget_logo" />
14+
</FrameLayout>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:id="@+id/widget_assistant_invisible"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:background="@android:color/transparent">
6+
7+
<ImageView
8+
android:layout_width="24dp"
9+
android:layout_height="24dp"
10+
android:layout_gravity="center"
11+
android:contentDescription="@string/assistant_open"
12+
android:src="@drawable/ic_widget_logo" />
13+
</FrameLayout>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:id="@+id/widget_assistant"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
android:background="@drawable/widget_background"
7+
android:gravity="center"
8+
android:orientation="vertical">
9+
10+
<ImageView
11+
android:layout_width="40dp"
12+
android:layout_height="40dp"
13+
android:contentDescription="@string/assistant_open"
14+
android:src="@drawable/ic_assistant"
15+
android:tint="@color/icon_color"
16+
tools:ignore="UseAppTint" />
17+
</LinearLayout>

0 commit comments

Comments
 (0)