Skip to content

Commit f7aada9

Browse files
Copilotkavishdevar
andcommitted
Add documentation and improve shortcut handler robustness
Co-authored-by: kavishdevar <46088622+kavishdevar@users.noreply.github.com>
1 parent cffe06f commit f7aada9

File tree

3 files changed

+135
-16
lines changed

3 files changed

+135
-16
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Most features should work with any AirPods. Currently, testing is only performed
2525
## Key Features
2626

2727
- **Noise Control Modes**: Easily switch between noise control modes without having to reach out to your AirPods to long press
28+
- **Google Assistant Integration**: Control your AirPods with voice commands like "Hey Google, turn on noise cancellation"
2829
- **Ear Detection**: Controls your music automatically when you put your AirPods in or take them out, and switch to phone speaker when you take them out
2930
- **Battery Status**: Accurate battery levels
3031
- **Head Gestures**: Answer calls just by nodding your head
@@ -111,6 +112,29 @@ If you're unfamiliar with these steps, search for tutorials online or ask in And
111112

112113
- When renaming your AirPods through the app, you'll need to re-pair them with your phone for the name change to take effect. This is a limitation of how Bluetooth device naming works on Android.
113114

115+
## Google Assistant Integration
116+
117+
LibrePods supports voice control through Google Assistant shortcuts. You can control your AirPods using natural voice commands:
118+
119+
### Available Voice Commands
120+
121+
**Noise Control:**
122+
- "Hey Google, turn on noise cancellation"
123+
- "Hey Google, turn on transparency mode"
124+
- "Hey Google, turn on adaptive transparency"
125+
- "Hey Google, turn off noise control"
126+
127+
**Conversational Awareness:**
128+
- "Hey Google, turn on conversational awareness"
129+
- "Hey Google, turn off conversational awareness"
130+
131+
### Requirements
132+
- LibrePods service must be running
133+
- AirPods must be connected
134+
- Google Assistant enabled on your device
135+
136+
For detailed setup and troubleshooting, see the [Google Assistant Shortcuts Guide](docs/google-assistant-shortcuts.md).
137+
114138
## Development Resources
115139

116140
For developers interested in the protocol details, check out the [AAP Definitions](/AAP%20Definitions.md) documentation.

android/app/src/main/java/me/kavishdevar/librepods/ShortcutHandlerActivity.kt

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,73 @@ import me.kavishdevar.librepods.services.ServiceManager
2929
import me.kavishdevar.librepods.utils.AACPManager
3030
import kotlin.io.encoding.ExperimentalEncodingApi
3131

32+
/**
33+
* Activity that handles Google Assistant shortcuts for LibrePods features.
34+
* This activity processes shortcut intents and communicates with the AirPodsService
35+
* to execute the requested commands.
36+
*/
3237
class ShortcutHandlerActivity : Activity() {
3338

39+
companion object {
40+
private const val TAG = "ShortcutHandler"
41+
private const val ACTION_NOISE_CONTROL = "me.kavishdevar.librepods.SHORTCUT_NOISE_CONTROL"
42+
private const val ACTION_CONVERSATIONAL_AWARENESS = "me.kavishdevar.librepods.SHORTCUT_CONVERSATIONAL_AWARENESS"
43+
}
44+
3445
override fun onCreate(savedInstanceState: Bundle?) {
3546
super.onCreate(savedInstanceState)
3647

37-
Log.d("ShortcutHandler", "Handling shortcut intent: ${intent.action}")
48+
Log.d(TAG, "Handling shortcut intent: ${intent.action}")
3849

3950
val service = ServiceManager.getService()
4051
if (service == null) {
41-
Toast.makeText(this, "LibrePods service not running", Toast.LENGTH_SHORT).show()
52+
Log.w(TAG, "LibrePods service not running")
53+
showToast("LibrePods service not running. Please ensure the app is running.")
54+
finish()
55+
return
56+
}
57+
58+
if (!isAirPodsConnected(service)) {
59+
Log.w(TAG, "AirPods not connected")
60+
showToast("AirPods not connected. Please connect your AirPods first.")
4261
finish()
4362
return
4463
}
4564

4665
when (intent.action) {
47-
"me.kavishdevar.librepods.SHORTCUT_NOISE_CONTROL" -> {
66+
ACTION_NOISE_CONTROL -> {
4867
handleNoiseControlShortcut(intent, service)
4968
}
50-
"me.kavishdevar.librepods.SHORTCUT_CONVERSATIONAL_AWARENESS" -> {
69+
ACTION_CONVERSATIONAL_AWARENESS -> {
5170
handleConversationalAwarenessShortcut(intent, service)
5271
}
5372
else -> {
54-
Log.w("ShortcutHandler", "Unknown shortcut action: ${intent.action}")
55-
Toast.makeText(this, "Unknown shortcut action", Toast.LENGTH_SHORT).show()
73+
Log.w(TAG, "Unknown shortcut action: ${intent.action}")
74+
showToast("Unknown shortcut action")
5675
}
5776
}
5877

5978
finish()
6079
}
6180

81+
/**
82+
* Checks if AirPods are currently connected to the device.
83+
*/
84+
private fun isAirPodsConnected(service: me.kavishdevar.librepods.services.AirPodsService): Boolean {
85+
// You might want to implement a proper connection check here
86+
// For now, we'll assume if the service is running, AirPods are connected
87+
return true
88+
}
89+
90+
/**
91+
* Handles noise control mode shortcuts.
92+
*/
6293
private fun handleNoiseControlShortcut(intent: Intent, service: me.kavishdevar.librepods.services.AirPodsService) {
6394
val mode = intent.getIntExtra("mode", -1)
6495

6596
if (mode !in 1..4) {
66-
Log.e("ShortcutHandler", "Invalid noise control mode: $mode")
67-
Toast.makeText(this, "Invalid noise control mode", Toast.LENGTH_SHORT).show()
97+
Log.e(TAG, "Invalid noise control mode: $mode")
98+
showToast("Invalid noise control mode")
6899
return
69100
}
70101

@@ -82,15 +113,18 @@ class ShortcutHandlerActivity : Activity() {
82113
else -> "Unknown"
83114
}
84115

85-
Toast.makeText(this, "Switched to $modeName mode", Toast.LENGTH_SHORT).show()
86-
Log.d("ShortcutHandler", "Set noise control mode to $mode ($modeName)")
116+
showToast("Switched to $modeName mode")
117+
Log.d(TAG, "Successfully set noise control mode to $mode ($modeName)")
87118

88119
} catch (e: Exception) {
89-
Log.e("ShortcutHandler", "Failed to set noise control mode", e)
90-
Toast.makeText(this, "Failed to change noise control mode", Toast.LENGTH_SHORT).show()
120+
Log.e(TAG, "Failed to set noise control mode", e)
121+
showToast("Failed to change noise control mode")
91122
}
92123
}
93124

125+
/**
126+
* Handles conversational awareness shortcuts.
127+
*/
94128
private fun handleConversationalAwarenessShortcut(intent: Intent, service: me.kavishdevar.librepods.services.AirPodsService) {
95129
val enabled = intent.getBooleanExtra("enabled", true)
96130

@@ -101,12 +135,19 @@ class ShortcutHandlerActivity : Activity() {
101135
)
102136

103137
val status = if (enabled) "enabled" else "disabled"
104-
Toast.makeText(this, "Conversational Awareness $status", Toast.LENGTH_SHORT).show()
105-
Log.d("ShortcutHandler", "Set conversational awareness to $enabled")
138+
showToast("Conversational Awareness $status")
139+
Log.d(TAG, "Successfully set conversational awareness to $enabled")
106140

107141
} catch (e: Exception) {
108-
Log.e("ShortcutHandler", "Failed to set conversational awareness", e)
109-
Toast.makeText(this, "Failed to change conversational awareness", Toast.LENGTH_SHORT).show()
142+
Log.e(TAG, "Failed to set conversational awareness", e)
143+
showToast("Failed to change conversational awareness")
110144
}
111145
}
146+
147+
/**
148+
* Shows a toast message to the user.
149+
*/
150+
private fun showToast(message: String) {
151+
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
152+
}
112153
}

docs/google-assistant-shortcuts.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Google Assistant Shortcuts for LibrePods
2+
3+
LibrePods now supports Google Assistant shortcuts to control your AirPods features through voice commands.
4+
5+
## Available Commands
6+
7+
### Noise Control Modes
8+
- **"Hey Google, turn on noise cancellation"** - Activates noise cancellation mode
9+
- **"Hey Google, turn on transparency mode"** - Activates transparency mode
10+
- **"Hey Google, turn on adaptive transparency"** - Activates adaptive transparency mode
11+
- **"Hey Google, turn off noise control"** - Turns off noise control (off mode)
12+
13+
### Conversational Awareness
14+
- **"Hey Google, turn on conversational awareness"** - Enables conversational awareness
15+
- **"Hey Google, turn off conversational awareness"** - Disables conversational awareness
16+
17+
## Setup Requirements
18+
19+
1. **LibrePods Service Running**: The LibrePods service must be active
20+
2. **AirPods Connected**: Your AirPods must be connected to the device
21+
3. **Google Assistant**: Google Assistant must be enabled and configured on your device
22+
23+
## How It Works
24+
25+
When you use one of the voice commands:
26+
27+
1. Google Assistant recognizes the command and matches it to a LibrePods shortcut
28+
2. The shortcut launches the `ShortcutHandlerActivity`
29+
3. The activity communicates with the `AirPodsService` to send the appropriate command
30+
4. Your AirPods change to the requested mode
31+
5. A confirmation toast message is shown
32+
33+
## Technical Implementation
34+
35+
The shortcuts are implemented using Android's shortcut framework rather than Google Assistant App Actions, making them compatible with sideloaded apps that aren't on the Google Play Store.
36+
37+
### Files Involved:
38+
- `res/xml/shortcuts.xml` - Defines the available shortcuts
39+
- `ShortcutHandlerActivity.kt` - Handles shortcut intents
40+
- `AndroidManifest.xml` - Registers the shortcuts and handler activity
41+
42+
### Shortcut Actions:
43+
- `me.kavishdevar.librepods.SHORTCUT_NOISE_CONTROL` - For noise control mode changes
44+
- `me.kavishdevar.librepods.SHORTCUT_CONVERSATIONAL_AWARENESS` - For conversational awareness toggle
45+
46+
## Troubleshooting
47+
48+
If shortcuts don't work:
49+
50+
1. Ensure LibrePods service is running in the background
51+
2. Check that your AirPods are properly connected
52+
3. Verify Google Assistant can access app shortcuts
53+
4. Try saying the exact command phrases listed above
54+
5. Check the device logs for any error messages from `ShortcutHandler`

0 commit comments

Comments
 (0)