Skip to content

Commit 83c5151

Browse files
implement settings activity for PreventAudioFocus
1 parent 87336a0 commit 83c5151

File tree

12 files changed

+171
-25
lines changed

12 files changed

+171
-25
lines changed

PreventAudioFocus/build.gradle.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
plugins {
22
alias(libs.plugins.buildlogic.android.application)
3+
alias(libs.plugins.buildlogic.kotlin.android)
34
}
45

56
android {
67
namespace = "com.programminghoch10.PreventAudioFocus"
78

89
defaultConfig {
910
minSdk = 23
10-
targetSdk = 35
11+
targetSdk = 36
1112
}
1213
}
14+
15+
dependencies {
16+
implementation(libs.androidx.fragment.ktx)
17+
implementation(libs.androidx.preference.ktx)
18+
}

PreventAudioFocus/src/main/AndroidManifest.xml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest
3-
xmlns:android="http://schemas.android.com/apk/res/android">
4-
5-
<application android:label="PreventAudioFocus">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<application android:label="@string/app_name">
5+
<activity
6+
android:name=".SettingsActivity"
7+
android:exported="true"
8+
android:label="@string/title_activity_settings"
9+
android:theme="@style/AppTheme"
10+
android:excludeFromRecents="true"
11+
>
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN" />
14+
<category android:name="de.robv.android.xposed.category.MODULE_SETTINGS" />
15+
</intent-filter>
16+
</activity>
17+
618
<meta-data
719
android:name="xposedmodule"
820
android:value="true"
921
/>
1022
<meta-data
1123
android:name="xposeddescription"
12-
android:value="Prevent apps from acquiring audio focus"
24+
android:value="@string/description"
1325
/>
1426
<meta-data
1527
android:name="xposedminversion"
16-
android:value="82"
28+
android:value="93"
1729
/>
1830
<meta-data
1931
android:name="xposedscope"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.programminghoch10.PreventAudioFocus
2+
3+
import android.media.AudioManager
4+
5+
val SHARED_PREFERENCES_NAME = "audiofocus"
6+
7+
val ENTRIES = AudioManager::class.java.declaredFields.filter { it.name.startsWith("AUDIOFOCUS_REQUEST") }.associate { it.name to it.getInt(null) }
8+
9+
val ENTRIES_DEFAULT = AudioManager.AUDIOFOCUS_REQUEST_GRANTED

PreventAudioFocus/src/main/java/com/programminghoch10/PreventAudioFocus/Hook.java

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.programminghoch10.PreventAudioFocus
2+
3+
import android.media.AudioManager
4+
import de.robv.android.xposed.IXposedHookLoadPackage
5+
import de.robv.android.xposed.XC_MethodReplacement
6+
import de.robv.android.xposed.XSharedPreferences
7+
import de.robv.android.xposed.XposedBridge
8+
import de.robv.android.xposed.XposedHelpers
9+
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam
10+
11+
class Hook : IXposedHookLoadPackage {
12+
override fun handleLoadPackage(lpparam: LoadPackageParam) {
13+
var clazz: Class<*>? = AudioManager::class.java
14+
if (lpparam.packageName == "android") clazz = XposedHelpers.findClass(
15+
"com.android.server.audio.MediaFocusControl", lpparam.classLoader
16+
)
17+
val sharedPreferences = XSharedPreferences(BuildConfig.APPLICATION_ID, SHARED_PREFERENCES_NAME)
18+
val constant = ENTRIES.map {
19+
Triple(
20+
it.key,
21+
it.value,
22+
sharedPreferences.getBoolean(it.key, false),
23+
)
24+
}.find { it.third }?.second ?: ENTRIES_DEFAULT
25+
XposedBridge.hookAllMethods(
26+
clazz, "requestAudioFocus", XC_MethodReplacement.returnConstant(constant)
27+
)
28+
}
29+
}
30+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.programminghoch10.PreventAudioFocus
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import androidx.preference.CheckBoxPreference
6+
7+
@SuppressLint("PrivateResource")
8+
class RadioPreference(context: Context) : CheckBoxPreference(context) {
9+
10+
init {
11+
widgetLayoutResource = R.layout.radio
12+
}
13+
14+
override fun setChecked(checked: Boolean) {
15+
if (isChecked) return
16+
super.setChecked(checked)
17+
}
18+
19+
fun onRadioPreferenceSelected(radioPreference: RadioPreference) {
20+
if (radioPreference == this) return
21+
super.setChecked(false)
22+
}
23+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.programminghoch10.PreventAudioFocus
2+
3+
import android.annotation.SuppressLint
4+
import android.os.Bundle
5+
import androidx.fragment.app.FragmentActivity
6+
import androidx.preference.PreferenceFragmentCompat
7+
8+
class SettingsActivity : FragmentActivity() {
9+
10+
override fun onCreate(savedInstanceState: Bundle?) {
11+
super.onCreate(savedInstanceState)
12+
setContentView(R.layout.settings_activity)
13+
if (savedInstanceState == null) {
14+
supportFragmentManager.beginTransaction().replace(R.id.settings, SettingsFragment()).commit()
15+
}
16+
actionBar?.setDisplayHomeAsUpEnabled(true)
17+
}
18+
19+
override fun onNavigateUp(): Boolean {
20+
finish()
21+
return true
22+
}
23+
24+
class SettingsFragment : PreferenceFragmentCompat() {
25+
@SuppressLint("WorldReadableFiles")
26+
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
27+
preferenceManager.sharedPreferencesName = SHARED_PREFERENCES_NAME
28+
preferenceManager.sharedPreferencesMode = MODE_WORLD_READABLE
29+
val context = requireContext()
30+
preferenceScreen = preferenceManager.createPreferenceScreen(context)
31+
32+
val radioPreferences = mutableListOf<RadioPreference>()
33+
for (entry in ENTRIES) {
34+
val preference = RadioPreference(context)
35+
preference.key = entry.key
36+
preference.title = entry.key
37+
preference.setDefaultValue(entry.value == ENTRIES_DEFAULT)
38+
radioPreferences.add(preference)
39+
preference.setOnPreferenceChangeListener { preference, newValue ->
40+
radioPreferences.forEach { it.onRadioPreferenceSelected(preference as RadioPreference) }
41+
true
42+
}
43+
preferenceScreen.addPreference(preference)
44+
}
45+
}
46+
}
47+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:id="@android:id/checkbox"
4+
style="@style/Widget.AppCompat.CompoundButton.RadioButton"
5+
android:background="@null"
6+
android:layout_width="wrap_content"
7+
android:layout_height="wrap_content"
8+
android:clickable="false"
9+
android:focusable="false" />
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:layout_width="match_parent"
3+
android:layout_height="match_parent"
4+
android:theme="@style/AppTheme.Edge2EdgeFix">
5+
6+
<FrameLayout
7+
android:id="@+id/settings"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent" />
10+
</LinearLayout>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<style name="AppTheme" parent="@android:style/Theme.DeviceDefault.Settings" />
4+
</resources>

0 commit comments

Comments
 (0)