Skip to content

Commit aca87af

Browse files
committed
Tile state based on digital assistant setup state
1 parent 5c95411 commit aca87af

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

app/src/main/java/com/wstxda/switchai/services/AssistantTileService.kt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.wstxda.switchai.services
22

33
import android.annotation.SuppressLint
44
import android.app.PendingIntent
5+
import android.app.role.RoleManager
56
import android.content.Intent
67
import android.os.Build
78
import android.service.quicksettings.Tile
@@ -46,10 +47,29 @@ class AssistantTileService : TileService() {
4647
private fun refreshTileContent() {
4748
val tile = qsTile ?: return
4849

50+
val isSetupDone = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
51+
getSystemService(RoleManager::class.java)?.isRoleHeld(RoleManager.ROLE_ASSISTANT) == true
52+
} else {
53+
preferenceHelper.getBoolean(Constants.IS_ASSIST_SETUP_DONE, false)
54+
}
55+
56+
if (!isSetupDone) {
57+
tile.state = Tile.STATE_UNAVAILABLE
58+
tile.updateTile()
59+
return
60+
}
61+
4962
val isSelectorEnabled =
5063
preferenceHelper.getBoolean(Constants.ASSISTANT_SELECTOR_DIALOG_PREF_KEY, false)
5164

52-
val assistantSubtitle = getString(R.string.assistant_label_open)
65+
val assistantValue =
66+
preferenceHelper.getString(Constants.DIGITAL_ASSISTANT_SELECT_PREF_KEY, null)
67+
68+
tile.state = if (isSelectorEnabled || assistantValue != null) {
69+
Tile.STATE_ACTIVE
70+
} else {
71+
Tile.STATE_INACTIVE
72+
}
5373

5474
if (isSelectorEnabled) {
5575
applyTileResources(
@@ -58,24 +78,22 @@ class AssistantTileService : TileService() {
5878
subtitle = getString(R.string.assistant_label_select),
5979
)
6080
} else {
61-
val assistantValue =
62-
preferenceHelper.getString(Constants.DIGITAL_ASSISTANT_SELECT_PREF_KEY, null)
6381
val iconRes = assistantResourcesManager.getAssistantIcon(assistantValue)
6482
val name = assistantResourcesManager.getAssistantName(assistantValue)
6583

6684
applyTileResources(
67-
icon = iconRes, title = name, subtitle = assistantSubtitle
85+
icon = iconRes,
86+
title = name,
87+
subtitle = getString(R.string.assistant_label_open),
6888
)
6989
}
7090

71-
tile.state = Tile.STATE_INACTIVE
7291
tile.updateTile()
7392
}
7493

7594
private fun applyTileResources(icon: Int, title: String, subtitle: String) {
7695
val tile = qsTile ?: return
7796
tile.icon = IconCompat.createWithResource(this, icon).toIcon(this)
78-
7997
tile.label = title
8098
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
8199
tile.subtitle = subtitle

0 commit comments

Comments
 (0)