Skip to content

Commit ed87623

Browse files
committed
android: add listening-mode cycle action to live update notification
1 parent 5694f1e commit ed87623

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

android/app/src/main/java/me/kavishdevar/librepods/services/AirPodsService.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
219219
private lateinit var sharedPreferencesLogs: SharedPreferences
220220
private lateinit var sharedPreferences: SharedPreferences
221221
private var wasConnectedForLive: Boolean = false
222+
223+
private fun currentListeningModeInt(): Int = ancNotification.status
222224
private val packetLogKey = "packet_log"
223225
private val _packetLogsFlow = MutableStateFlow<Set<String>>(emptySet())
224226
val packetLogsFlow: StateFlow<Set<String>> get() = _packetLogsFlow
@@ -362,7 +364,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
362364
this@AirPodsService.applicationContext,
363365
name,
364366
battery,
365-
headsUp = true
367+
headsUp = true,
368+
currentListeningMode = currentListeningModeInt()
366369
)
367370
wasConnectedForLive = true
368371
}
@@ -1711,7 +1714,8 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
17111714
service.applicationContext,
17121715
name,
17131716
battery,
1714-
headsUp = true
1717+
headsUp = true,
1718+
currentListeningMode = currentListeningModeInt()
17151719
)
17161720
}
17171721
}
@@ -2092,11 +2096,12 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
20922096
val hasMeaningfulBattery = batteryList.any {
20932097
it.status != BatteryStatus.DISCONNECTED && it.level > 0
20942098
}
2099+
val mode = currentListeningModeInt()
20952100
if (!wasConnectedForLive && hasMeaningfulBattery) {
2096-
LiveUpdateNotification.show(this, resolvedName, batteryList, headsUp = true)
2101+
LiveUpdateNotification.show(this, resolvedName, batteryList, headsUp = true, currentListeningMode = mode)
20972102
wasConnectedForLive = true
20982103
} else if (wasConnectedForLive) {
2099-
LiveUpdateNotification.update(this, resolvedName, batteryList)
2104+
LiveUpdateNotification.update(this, resolvedName, batteryList, currentListeningMode = mode)
21002105
}
21012106
LiveUpdateNotification.checkLowBattery(this, batteryList)
21022107
notificationManager.cancel(1)

android/app/src/main/java/me/kavishdevar/librepods/services/notifications/LiveUpdateNotification.kt

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,22 @@ object LiveUpdateNotification {
4949
context: Context,
5050
airpodsName: String,
5151
batteryList: List<Battery>,
52-
headsUp: Boolean
52+
headsUp: Boolean,
53+
currentListeningMode: Int
5354
) {
5455
val nm = context.getSystemService(NotificationManager::class.java)
55-
val builder = buildMain(context, airpodsName, batteryList, headsUp)
56+
val builder = buildMain(context, airpodsName, batteryList, headsUp, currentListeningMode)
5657
nm.notify(NOTIF_ID_MAIN, builder.build())
5758
}
5859

5960
fun update(
6061
context: Context,
6162
airpodsName: String,
62-
batteryList: List<Battery>
63+
batteryList: List<Battery>,
64+
currentListeningMode: Int
6365
) {
6466
val nm = context.getSystemService(NotificationManager::class.java)
65-
val builder = buildMain(context, airpodsName, batteryList, headsUp = false)
67+
val builder = buildMain(context, airpodsName, batteryList, headsUp = false, currentListeningMode)
6668
nm.notify(NOTIF_ID_MAIN, builder.build())
6769
}
6870

@@ -125,7 +127,7 @@ object LiveUpdateNotification {
125127
newMode: Byte
126128
) {
127129
if (!state.shouldFireListeningModeChange(newMode)) return
128-
show(context, airpodsName, batteryList, headsUp = true)
130+
show(context, airpodsName, batteryList, headsUp = true, currentListeningMode = newMode.toInt())
129131
}
130132

131133
fun cancelAll(context: Context) {
@@ -141,7 +143,8 @@ object LiveUpdateNotification {
141143
context: Context,
142144
airpodsName: String,
143145
batteryList: List<Battery>,
144-
headsUp: Boolean
146+
headsUp: Boolean,
147+
currentListeningMode: Int
145148
): NotificationCompat.Builder {
146149
val left = batteryList.firstOrNull { it.component == BatteryComponent.LEFT }
147150
val right = batteryList.firstOrNull { it.component == BatteryComponent.RIGHT }
@@ -192,6 +195,27 @@ object LiveUpdateNotification {
192195
.setOnlyAlertOnce(!headsUp)
193196
.setRequestPromotedOngoing(true)
194197
.addExtras(samsungLiveExtras)
198+
.addAction(
199+
listeningModeIconRes(currentListeningMode),
200+
context.getString(listeningModeLabelRes(currentListeningMode)),
201+
listeningModePendingIntent(context)
202+
)
203+
}
204+
205+
private fun listeningModeLabelRes(mode: Int): Int = when (mode) {
206+
1 -> R.string.off
207+
2 -> R.string.noise_cancellation
208+
3 -> R.string.transparency
209+
4 -> R.string.adaptive
210+
else -> R.string.noise_control
211+
}
212+
213+
private fun listeningModeIconRes(mode: Int): Int = when (mode) {
214+
1 -> R.drawable.close
215+
2 -> R.drawable.noise_cancellation
216+
3 -> R.drawable.transparency
217+
4 -> R.drawable.adaptive
218+
else -> R.drawable.noise_cancellation
195219
}
196220

197221
private fun mainActivityPendingIntent(context: Context): PendingIntent {
@@ -202,4 +226,16 @@ object LiveUpdateNotification {
202226
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
203227
)
204228
}
229+
230+
private fun listeningModePendingIntent(context: Context): PendingIntent {
231+
val intent = Intent("me.kavishdevar.librepods.SET_ANC_MODE").apply {
232+
setPackage(context.packageName)
233+
}
234+
return PendingIntent.getBroadcast(
235+
context,
236+
2,
237+
intent,
238+
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
239+
)
240+
}
205241
}

0 commit comments

Comments
 (0)