Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Add option to ignore audio focus #380

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ apply plugin: 'kotlin-parcelize'


android {
compileSdkVersion 32
// buildToolsVersion is optional because the plugin uses a recommended version by default

defaultConfig {
applicationId 'org.y20k.transistor'
namespace "org.y20k.transistor"
minSdkVersion 25
compileSdk 32
targetSdkVersion 32
versionCode 91
versionName '4.1.1'
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.y20k.transistor">
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/org/y20k/transistor/Keys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ object Keys {
const val CMD_CANCEL_SLEEP_TIMER: String = "CANCEL_SLEEP_TIMER"
const val CMD_PLAY_STREAM: String = "PLAY_STREAM"

const val CMD_UPDATE_AUDIO_FOCUS_SETTING: String = "UPDATE_AUDIO_FOCUS_SETTING"

// preferences
const val PREF_RADIO_BROWSER_API: String = "RADIO_BROWSER_API"
const val PREF_ONE_TIME_HOUSEKEEPING_NECESSARY: String = "ONE_TIME_HOUSEKEEPING_NECESSARY_VERSIONCODE_72" // increment to current app version code to trigger housekeeping that runs only once
Expand All @@ -92,6 +94,7 @@ object Keys {
const val PREF_EDIT_STATIONS: String = "EDIT_STATIONS"
const val PREF_EDIT_STREAMS_URIS: String = "EDIT_STREAMS_URIS"

const val PREF_HANDLE_AUDIO_FOCUS: String = "PREF_HANDLE_AUDIO_FOCUS"

// states
const val STATE_SLEEP_TIMER_STOPPED: Int = 0
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/org/y20k/transistor/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package org.y20k.transistor

import android.content.SharedPreferences
import android.os.Bundle
import android.support.v4.media.session.MediaControllerCompat
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.navigation.fragment.NavHostFragment
Expand Down Expand Up @@ -96,6 +97,9 @@ class MainActivity: AppCompatActivity() {
Keys.PREF_THEME_SELECTION -> {
AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection())
}
Keys.PREF_HANDLE_AUDIO_FOCUS -> {
MediaControllerCompat.getMediaController(this).sendCommand(Keys.CMD_UPDATE_AUDIO_FOCUS_SETTING, null, null)
}
}
}
/*
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/org/y20k/transistor/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ class SettingsFragment: PreferenceFragmentCompat(), YesNoDialog.YesNoDialogListe
}
}

// set up "Handle Audio Focus" preference
val preferenceHandleAudioFocus: CheckBoxPreference = CheckBoxPreference(activity as Context)
preferenceHandleAudioFocus.title = getString(R.string.pref_handle_audio_focus_title)
preferenceHandleAudioFocus.key = Keys.PREF_HANDLE_AUDIO_FOCUS
preferenceHandleAudioFocus.summary = getString(R.string.pref_handle_audio_focus_summary)
preferenceHandleAudioFocus.setDefaultValue(true)


// set up "Update Station Images" preference
val preferenceUpdateStationImages: Preference = Preference(activity as Context)
preferenceUpdateStationImages.title = getString(R.string.pref_update_station_images_title)
Expand Down Expand Up @@ -209,6 +217,7 @@ class SettingsFragment: PreferenceFragmentCompat(), YesNoDialog.YesNoDialogListe
val preferenceCategoryGeneral: PreferenceCategory = PreferenceCategory(activity as Context)
preferenceCategoryGeneral.title = getString(R.string.pref_general_title)
preferenceCategoryGeneral.contains(preferenceThemeSelection)
preferenceCategoryGeneral.contains(preferenceHandleAudioFocus)

val preferenceCategoryMaintenance: PreferenceCategory = PreferenceCategory(activity as Context)
preferenceCategoryMaintenance.title = getString(R.string.pref_maintenance_title)
Expand All @@ -232,6 +241,7 @@ class SettingsFragment: PreferenceFragmentCompat(), YesNoDialog.YesNoDialogListe
// setup preference screen
screen.addPreference(preferenceCategoryGeneral)
screen.addPreference(preferenceThemeSelection)
screen.addPreference(preferenceHandleAudioFocus)
screen.addPreference(preferenceCategoryMaintenance)
screen.addPreference(preferenceUpdateStationImages)
// screen.addPreference(preferenceUpdateCollection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,8 @@ object PreferencesHelper {
return sharedPreferences.getBoolean(Keys.PREF_DOWNLOAD_OVER_MOBILE, Keys.DEFAULT_DOWNLOAD_OVER_MOBILE)
}

/* Loads handling of audio focus */
fun loadHandleAudioFocus(): Boolean {
return sharedPreferences.getBoolean(Keys.PREF_HANDLE_AUDIO_FOCUS, true)
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/org/y20k/transistor/playback/PlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class PlayerService(): MediaBrowserServiceCompat() {
private lateinit var sleepTimer: CountDownTimer
private var sleepTimerTimeRemaining: Long = 0L
private var playbackRestartCounter: Int = 0
private var handleAudioFocus: Boolean = true

private val attributes = AudioAttributes.Builder()
.setContentType(C.CONTENT_TYPE_MUSIC)
Expand Down Expand Up @@ -127,6 +128,10 @@ class PlayerService(): MediaBrowserServiceCompat() {
// fetch the metadata history
metadataHistory = PreferencesHelper.loadMetadataHistory()

// fetch handle audio focus setting
handleAudioFocus = PreferencesHelper.loadHandleAudioFocus()
player.setAudioAttributes(attributes, handleAudioFocus)

// create a new MediaSession
createMediaSession()

Expand Down Expand Up @@ -740,6 +745,15 @@ class PlayerService(): MediaBrowserServiceCompat() {
preparePlayer(true)
return true
}
Keys.CMD_UPDATE_AUDIO_FOCUS_SETTING -> {
if (player is SimpleExoPlayer) {
handleAudioFocus = PreferencesHelper.loadHandleAudioFocus()
player.setAudioAttributes(attributes, handleAudioFocus)
return true
} else {
return false
}
}
else -> {
return false
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
<string name="pref_theme_selection_mode_device_default">Identisch zum Gerät</string>
<string name="pref_theme_selection_mode_dark">Dunkler Modus</string>
<string name="pref_general_title">Allgemein</string>
<string name="pref_handle_audio_focus_title">Audio Focus berücksichtigen</string>
<string name="pref_handle_audio_focus_summary">Wiedergabe stoppen wenn andere Anwendung Audio ausgibt oder Telefonanruf gestartet wird</string>
<string name="pref_delete_all_title">Downloads löschen</string>
<string name="toastmessage_error_missing_storage_permission">Erteile die Berechtigung „Speicher lesen“, um diese Datei zu öffnen.</string>
<string name="dialog_yes_no_positive_button_update_covers">Aktualisieren</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
<string name="pref_edit_station_stream_summary_disabled">Editing of streaming links disabled. </string>
<string name="pref_edit_station_stream_title">Edit Streaming Links</string>
<string name="pref_general_title">General</string>
<string name="pref_handle_audio_focus_title">Handle Audio Focus</string>
<string name="pref_handle_audio_focus_summary">Stop playback when other app starts playback or phone call is started</string>
<string name="pref_maintenance_title">Maintenance</string>
<string name="pref_m3u_export_summary">Save your radio stations to an M3U playlist file that can be imported into other players.</string>
<string name="pref_m3u_export_title">Export M3U</string>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.2.0'
classpath 'com.android.tools.build:gradle:8.6.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ org.gradle.jvmargs=-Xmx1536m

# enables androidx repo
android.useAndroidX=true
android.enableJetifier=true
android.enableJetifier=true
android.defaults.buildfeatures.buildconfig=true
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Jan 28 14:15:43 CET 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Empty file modified gradlew
100644 → 100755
Empty file.