Skip to content
Merged
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
8 changes: 7 additions & 1 deletion PreventAudioFocus/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
plugins {
alias(libs.plugins.buildlogic.android.application)
alias(libs.plugins.buildlogic.kotlin.android)
}

android {
namespace = "com.programminghoch10.PreventAudioFocus"

defaultConfig {
minSdk = 23
targetSdk = 35
targetSdk = 36
}
}

dependencies {
implementation(libs.androidx.fragment.ktx)
implementation(libs.androidx.preference.ktx)
}
19 changes: 16 additions & 3 deletions PreventAudioFocus/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android">

<application android:label="PreventAudioFocus">
<application android:label="@string/app_name">
<activity
android:name=".SettingsActivity"
android:excludeFromRecents="true"
android:exported="true"
android:label="@string/title_activity_settings"
android:theme="@style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="de.robv.android.xposed.category.MODULE_SETTINGS" />
</intent-filter>
</activity>

<meta-data
android:name="xposedmodule"
android:value="true"
/>
<meta-data
android:name="xposeddescription"
android:value="Prevent apps from acquiring audio focus"
android:value="@string/description"
/>
<meta-data
android:name="xposedminversion"
android:value="82"
android:value="93"
/>
<meta-data
android:name="xposedscope"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.programminghoch10.PreventAudioFocus

import android.media.AudioManager

val SHARED_PREFERENCES_NAME = "audiofocus"

val ENTRIES = AudioManager::class.java.declaredFields.filter { it.name.startsWith("AUDIOFOCUS_REQUEST") }.associate { it.name to it.getInt(null) }

val ENTRIES_DEFAULT = AudioManager.AUDIOFOCUS_REQUEST_GRANTED

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.programminghoch10.PreventAudioFocus

import android.media.AudioManager
import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.XC_MethodReplacement
import de.robv.android.xposed.XSharedPreferences
import de.robv.android.xposed.XposedBridge
import de.robv.android.xposed.XposedHelpers
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam

class Hook : IXposedHookLoadPackage {
override fun handleLoadPackage(lpparam: LoadPackageParam) {
var clazz: Class<*>? = AudioManager::class.java
if (lpparam.packageName == "android") clazz = XposedHelpers.findClass(
"com.android.server.audio.MediaFocusControl", lpparam.classLoader
)
val sharedPreferences = XSharedPreferences(BuildConfig.APPLICATION_ID, SHARED_PREFERENCES_NAME)
val constant = ENTRIES.map {
Triple(
it.key,
it.value,
sharedPreferences.getBoolean(it.key, false),
)
}.find { it.third }?.second ?: ENTRIES_DEFAULT
XposedBridge.hookAllMethods(
clazz, "requestAudioFocus", XC_MethodReplacement.returnConstant(constant)
)
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.programminghoch10.PreventAudioFocus

import android.annotation.SuppressLint
import android.content.Context
import androidx.preference.CheckBoxPreference

@SuppressLint("PrivateResource")
class RadioPreference(context: Context) : CheckBoxPreference(context) {

init {
widgetLayoutResource = R.layout.radio
}

override fun setChecked(checked: Boolean) {
if (isChecked) return
super.setChecked(checked)
}

fun onRadioPreferenceSelected(radioPreference: RadioPreference) {
if (radioPreference == this) return
super.setChecked(false)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.programminghoch10.PreventAudioFocus

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import androidx.preference.PreferenceFragmentCompat

class SettingsActivity : FragmentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.settings_activity)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction().replace(R.id.settings, SettingsFragment()).commit()
}
actionBar?.setDisplayHomeAsUpEnabled(true)
}

override fun onNavigateUp(): Boolean {
finish()
return true
}

class SettingsFragment : PreferenceFragmentCompat() {
@SuppressLint("WorldReadableFiles")
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
preferenceManager.sharedPreferencesName = SHARED_PREFERENCES_NAME
preferenceManager.sharedPreferencesMode = MODE_WORLD_READABLE
val context = requireContext()
preferenceScreen = preferenceManager.createPreferenceScreen(context)

val radioPreferences = mutableListOf<RadioPreference>()
for (entry in ENTRIES) {
val preference = RadioPreference(context)
preference.key = entry.key
preference.title = entry.key
preference.setDefaultValue(entry.value == ENTRIES_DEFAULT)
radioPreferences.add(preference)
preference.setOnPreferenceChangeListener { preference, newValue ->
radioPreferences.forEach { it.onRadioPreferenceSelected(preference as RadioPreference) }
true
}
preferenceScreen.addPreference(preference)
}
}
}
}
11 changes: 11 additions & 0 deletions PreventAudioFocus/src/main/res/layout/radio.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RadioButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/checkbox"
style="@style/Widget.AppCompat.CompoundButton.RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:clickable="false"
android:focusable="false"
/>
13 changes: 13 additions & 0 deletions PreventAudioFocus/src/main/res/layout/settings_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme.Edge2EdgeFix"
>

<FrameLayout
android:id="@+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
5 changes: 5 additions & 0 deletions PreventAudioFocus/src/main/res/values-v21/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="AppTheme" parent="@android:style/Theme.DeviceDefault.Settings" />
</resources>
5 changes: 5 additions & 0 deletions PreventAudioFocus/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<string name="app_name">PreventAudioFocus</string>
<string name="title_activity_settings">PreventAudioFocus Settings</string>
<string name="description">Prevent apps from acquiring audio focus</string>
</resources>
9 changes: 9 additions & 0 deletions PreventAudioFocus/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="AppTheme" parent="@android:style/Theme.DeviceDefault" />

<style name="AppTheme.Edge2EdgeFix">
<item name="android:fitsSystemWindows">true</item>
</style>
</resources>