|
| 1 | +package de.binarynoise.HideAbi |
| 2 | + |
| 3 | +import kotlin.contracts.ExperimentalContracts |
| 4 | +import kotlin.contracts.InvocationKind |
| 5 | +import kotlin.contracts.contract |
| 6 | +import android.content.Context |
| 7 | +import android.os.Bundle |
| 8 | +import androidx.fragment.app.FragmentActivity |
| 9 | +import androidx.preference.CheckBoxPreference |
| 10 | +import androidx.preference.Preference |
| 11 | +import androidx.preference.PreferenceCategory |
| 12 | +import androidx.preference.PreferenceFragmentCompat |
| 13 | +import androidx.preference.PreferenceGroup |
| 14 | +import androidx.preference.children |
| 15 | +import de.binarynoise.HideAbi.BuildConfig.SHARED_PREFERENCES_NAME |
| 16 | +import de.binarynoise.reflection.cast |
| 17 | + |
| 18 | + |
| 19 | +class SettingsActivity : FragmentActivity() { |
| 20 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 21 | + super.onCreate(savedInstanceState) |
| 22 | + setContentView(R.layout.settings_activity) |
| 23 | + if (savedInstanceState == null) { |
| 24 | + supportFragmentManager.beginTransaction().replace(R.id.settings, SettingsFragment()).commit() |
| 25 | + } |
| 26 | + actionBar?.setDisplayHomeAsUpEnabled(true) |
| 27 | + } |
| 28 | + |
| 29 | + override fun onNavigateUp(): Boolean { |
| 30 | + finishAndRemoveTask() |
| 31 | + return true |
| 32 | + } |
| 33 | + |
| 34 | + class SettingsFragment : PreferenceFragmentCompat() { |
| 35 | + |
| 36 | + |
| 37 | + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { |
| 38 | + preferenceManager.sharedPreferencesName = SHARED_PREFERENCES_NAME |
| 39 | + preferenceManager.sharedPreferencesMode = MODE_WORLD_READABLE |
| 40 | + |
| 41 | + val ctx = preferenceManager.context |
| 42 | + preferenceScreen = preferenceManager.createPreferenceScreen(ctx) |
| 43 | + preferenceScreen.apply { |
| 44 | + for (category in AbiCategory.entries) { |
| 45 | + addPreference(PreferenceCategory(ctx)) { |
| 46 | + title = category.name |
| 47 | + val abis = ctx.getHardwareABIs(category) |
| 48 | + for (abi in abis) { |
| 49 | + addPreference(CheckBoxPreference(ctx)) { |
| 50 | + key = category.getPreferenceKeyFor(abi) |
| 51 | + title = abi |
| 52 | + summaryOn = "This ABI will be hidden and not reported to apps" |
| 53 | + summaryOff = "This ABI will be reported to apps" |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + setIconSpaceReservedRecursive(false) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns the hardware ABIs for the given property. |
| 64 | + * Combines the ABIs we currently see with the ones that are hooked from the preferences (and can't see). |
| 65 | + */ |
| 66 | + fun Context.getHardwareABIs(property: AbiCategory): Set<String> { |
| 67 | + val sharedPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_WORLD_READABLE) |
| 68 | + |
| 69 | + return (property.getter() + sharedPreferences.all // |
| 70 | + .filter { (k, v) -> k.startsWith(property.property) && (v?.cast<Boolean?>() == true) } // |
| 71 | + .keys // |
| 72 | + .map { it.substringAfter("_") } // |
| 73 | + .toTypedArray()) // |
| 74 | + .toSet() |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + @OptIn(ExperimentalContracts::class) |
| 79 | + inline fun <T : Preference> PreferenceGroup.addPreference(preference: T, setup: T.() -> Unit) { |
| 80 | + contract { |
| 81 | + callsInPlace(setup, InvocationKind.EXACTLY_ONCE) |
| 82 | + } |
| 83 | + |
| 84 | + val isPreferenceGroup = preference is PreferenceGroup |
| 85 | + |
| 86 | + if (isPreferenceGroup) { |
| 87 | + // PreferenceGroup needs to be added to the tree before other preferences can be added to it |
| 88 | + addPreference(preference) |
| 89 | + } |
| 90 | + |
| 91 | + preference.apply(setup) |
| 92 | + |
| 93 | + if (!isPreferenceGroup) { |
| 94 | + // normal preferences need the setup applied before being added to the tree |
| 95 | + addPreference(preference) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private fun Preference.setIconSpaceReservedRecursive(iconSpaceReserved: Boolean = false) { |
| 100 | + this.isIconSpaceReserved = iconSpaceReserved |
| 101 | + if (this is PreferenceGroup) this.children.forEach { it.setIconSpaceReservedRecursive(iconSpaceReserved) } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments