Skip to content

Commit e5906b6

Browse files
Implement SensorMod (#21)
1 parent d87ab89 commit e5906b6

File tree

15 files changed

+211
-0
lines changed

15 files changed

+211
-0
lines changed

SensorMod/Readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# SensorMod
2+
3+
This Module allows you to selectively disable sensors on your android device.
4+
Every sensor is listed and can be selectively disabled at choice.
5+
Supports all sensors reported by Android through the SensorManager API.
6+
Select all apps to disable the sensors for as scope for the module.

SensorMod/build.gradle.kts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
alias(libs.plugins.buildlogic.android.application)
3+
}
4+
5+
android {
6+
namespace = "com.programminghoch10.SensorMod"
7+
8+
defaultConfig {
9+
minSdk = 16
10+
targetSdk = 35
11+
multiDexEnabled = true
12+
}
13+
compileOptions {
14+
isCoreLibraryDesugaringEnabled = true
15+
}
16+
}
17+
18+
dependencies {
19+
implementation(libs.androidx.preference)
20+
coreLibraryDesugaring(libs.android.desugarJdkLibs)
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest
3+
xmlns:android="http://schemas.android.com/apk/res/android">
4+
5+
<application android:label="@string/app_name">
6+
<activity
7+
android:name=".SettingsActivity"
8+
android:exported="true"
9+
android:label="@string/title_activity_settings"
10+
android:theme="@style/AppTheme"
11+
>
12+
<intent-filter>
13+
<action android:name="android.intent.action.APPLICATION_PREFERENCES" />
14+
<category android:name="android.intent.category.DEFAULT" />
15+
<action android:name="android.intent.action.MAIN" />
16+
<category android:name="de.robv.android.xposed.category.MODULE_SETTINGS" />
17+
</intent-filter>
18+
</activity>
19+
20+
<meta-data
21+
android:name="xposedmodule"
22+
android:value="true"
23+
/>
24+
<meta-data
25+
android:name="xposeddescription"
26+
android:value="@string/description"
27+
/>
28+
<meta-data
29+
android:name="xposedminversion"
30+
android:value="93"
31+
/>
32+
</application>
33+
</manifest>

SensorMod/src/main/assets/xposed_init

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.programminghoch10.SensorMod.Hook
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.programminghoch10.SensorMod;
2+
3+
import android.hardware.Sensor;
4+
5+
import java.util.Objects;
6+
7+
public class Common {
8+
9+
public static boolean DEFAULT_SENSOR_STATE = true;
10+
11+
public static String getKey(Sensor sensor) {
12+
return "sensor_" + Objects.hash(sensor.getName(), sensor.getId(), sensor.getVendor(), sensor.getVersion(), sensor.getType());
13+
}
14+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.programminghoch10.SensorMod;
2+
3+
import static com.programminghoch10.SensorMod.Common.DEFAULT_SENSOR_STATE;
4+
import static com.programminghoch10.SensorMod.Common.getKey;
5+
6+
import android.content.SharedPreferences;
7+
import android.hardware.Sensor;
8+
import android.hardware.SensorManager;
9+
import android.util.Log;
10+
11+
import java.util.List;
12+
13+
import de.robv.android.xposed.IXposedHookLoadPackage;
14+
import de.robv.android.xposed.XC_MethodHook;
15+
import de.robv.android.xposed.XSharedPreferences;
16+
import de.robv.android.xposed.XposedHelpers;
17+
import de.robv.android.xposed.callbacks.XC_LoadPackage;
18+
19+
public class Hook implements IXposedHookLoadPackage {
20+
21+
@Override
22+
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
23+
if (lpparam.packageName.equals(BuildConfig.APPLICATION_ID)) return;
24+
SharedPreferences sharedPreferences = new XSharedPreferences(BuildConfig.APPLICATION_ID, "sensors");
25+
Class<SensorManager> systemSensorManager = (Class<SensorManager>) XposedHelpers.findClass("android.hardware.SystemSensorManager", lpparam.classLoader);
26+
XposedHelpers.findAndHookMethod(systemSensorManager, "getFullSensorList", new XC_MethodHook() {
27+
@Override
28+
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
29+
if (param.hasThrowable()) return;
30+
Log.d("Logger", "afterHookedMethod: filter getFullSensorList");
31+
List<Sensor> result = (List<Sensor>) param.getResult();
32+
result = result.stream().filter(sensor -> sharedPreferences.getBoolean(getKey(sensor), DEFAULT_SENSOR_STATE)).toList();
33+
param.setResult(result);
34+
}
35+
});
36+
Log.d("Logger", "handleLoadPackage: Hooked SystemSensorManager");
37+
}
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.programminghoch10.SensorMod;
2+
3+
import static com.programminghoch10.SensorMod.Common.DEFAULT_SENSOR_STATE;
4+
import static com.programminghoch10.SensorMod.Common.getKey;
5+
6+
import android.annotation.SuppressLint;
7+
import android.app.ActionBar;
8+
import android.hardware.Sensor;
9+
import android.hardware.SensorManager;
10+
import android.os.Bundle;
11+
12+
import androidx.fragment.app.FragmentActivity;
13+
import androidx.preference.PreferenceFragmentCompat;
14+
import androidx.preference.PreferenceManager;
15+
import androidx.preference.PreferenceScreen;
16+
import androidx.preference.SwitchPreference;
17+
18+
import java.util.List;
19+
20+
public class SettingsActivity extends FragmentActivity {
21+
@Override
22+
protected void onCreate(Bundle savedInstanceState) {
23+
super.onCreate(savedInstanceState);
24+
setContentView(R.layout.settings_activity);
25+
if (savedInstanceState == null) {
26+
getSupportFragmentManager()
27+
.beginTransaction()
28+
.replace(R.id.settings, new SettingsFragment())
29+
.commit();
30+
}
31+
ActionBar actionBar = getActionBar();
32+
if (actionBar != null) {
33+
actionBar.setDisplayHomeAsUpEnabled(
34+
getSupportFragmentManager().getBackStackEntryCount() > 0
35+
);
36+
}
37+
}
38+
39+
public static class SettingsFragment extends PreferenceFragmentCompat {
40+
@SuppressLint("WorldReadableFiles")
41+
@Override
42+
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
43+
PreferenceManager preferenceManager = getPreferenceManager();
44+
setPreferenceScreen(preferenceManager.createPreferenceScreen(requireContext()));
45+
preferenceManager.setSharedPreferencesName("sensors");
46+
preferenceManager.setSharedPreferencesMode(MODE_WORLD_READABLE);
47+
PreferenceScreen preferenceScreen = getPreferenceScreen();
48+
SensorManager sensorManager = (SensorManager) requireContext().getSystemService(SENSOR_SERVICE);
49+
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
50+
for (Sensor sensor : sensors) {
51+
SwitchPreference preference = new SwitchPreference(requireContext());
52+
preference.setDefaultValue(DEFAULT_SENSOR_STATE);
53+
preference.setKey(getKey(sensor));
54+
preference.setTitle(sensor.getName());
55+
preference.setSummary(getKey(sensor) + " - " + sensor);
56+
preferenceScreen.addPreference(preference);
57+
}
58+
}
59+
}
60+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="match_parent"
6+
style="@style/AppTheme.Edge2EdgeFix"
7+
>
8+
9+
<FrameLayout
10+
android:id="@+id/settings"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent"
13+
/>
14+
</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>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<string name="app_name">SensorMod</string>
3+
<string name="description">Selectively disable sensors.</string>
4+
<string name="title_activity_settings">SensorMod Settings</string>
5+
</resources>

0 commit comments

Comments
 (0)