File tree Expand file tree Collapse file tree 4 files changed +168
-112
lines changed
Expand file tree Collapse file tree 4 files changed +168
-112
lines changed Original file line number Diff line number Diff line change 108108 <service
109109 android : name =" .service.RecordingService"
110110 android : enabled =" true"
111- android : exported =" false"
112- android : foregroundServiceType =" microphone" >
111+ android : exported =" true"
112+ android : foregroundServiceType =" microphone"
113+ tools : ignore =" ExportedService" >
113114 <intent-filter android : priority =" 2147483647" >
114115 <action android : name =" android.intent.action.MEDIA_BUTTON" />
115116 </intent-filter >
117+ <intent-filter >
118+ <action android : name =" android.media.browse.MediaBrowserService" />
119+ </intent-filter >
116120 </service >
117121
118- <!-- Both receivers must remain: the system persists the last known media button
119- receiver ComponentName across reboots. Different builds registered different
120- receivers, so we keep both to ensure delivery regardless of which one the
121- system has cached. Both forward to RecordingService. -->
122- <!-- android:priority="2147483647" (the maximum integer value) tells the Android system that ilseon's manifest-registered receivers should be considered first if no other app is currently "owning" the media controls. -->
123122 <receiver android : name =" .service.MediaButtonBroadcastReceiver"
124123 android : exported =" true" >
125124 <intent-filter android : priority =" 2147483647" >
Original file line number Diff line number Diff line change @@ -168,6 +168,8 @@ class MainActivity : ComponentActivity() {
168168 } catch (e: Exception ) {
169169 Log .e(" MainActivity" , " Failed to publish shortcuts" , e)
170170 }
171+ // Automatically re-assert media priority when app is opened
172+ settingsViewModel.refreshMediaPriority()
171173 }
172174
173175 setContent {
@@ -827,6 +829,8 @@ class MainActivity : ComponentActivity() {
827829 override fun onNewIntent (intent : Intent ) {
828830 super .onNewIntent(intent)
829831 intentState.value = intent
832+ // Refresh priority when returning to the app
833+ settingsViewModel.refreshMediaPriority()
830834 }
831835
832836 private fun publishShortcuts (context : Context ) {
Original file line number Diff line number Diff line change @@ -87,11 +87,30 @@ class SettingsViewModel @Inject constructor(
8787 }
8888 }
8989
90+ /* *
91+ * Pings the RecordingService to re-assert its MediaSession and AudioFocus priority.
92+ * This helps "win" priority back from aggressive apps like Spotify without
93+ * requiring a full toggle of the setting.
94+ */
95+ fun refreshMediaPriority () {
96+ viewModelScope.launch {
97+ if (settingsRepository.mediaButtonTriggerEnabled.first()) {
98+ val serviceIntent = Intent (context, RecordingService ::class .java).apply {
99+ action = " REFRESH_PRIORITY"
100+ }
101+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
102+ context.startForegroundService(serviceIntent)
103+ } else {
104+ context.startService(serviceIntent)
105+ }
106+ }
107+ }
108+ }
109+
90110 fun setMediaButtonTriggerEnabled (enabled : Boolean ) {
91111 viewModelScope.launch {
92112 settingsRepository.setMediaButtonTriggerEnabled(enabled)
93113 }
94- // Start/stop the RecordingService so the MediaSession is alive to receive events
95114 val serviceIntent = Intent (context, RecordingService ::class .java)
96115 if (enabled) {
97116 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
You can’t perform that action at this time.
0 commit comments