package ru.abusalimov.xposed.lightsensorfilter;

import android.util.Log;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

public class LightSensorFilter implements IXposedHookLoadPackage {

	protected static final int ALS_HANDLE = 1;
	protected static final float BOGUS_LUX_VALUE = 30000.0f;

	@Override
	public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
		XposedHelpers.findAndHookMethod(
				"android.hardware.SystemSensorManager$SensorEventQueue",
				lpparam.classLoader,
				"dispatchSensorEvent",
				int.class, float[].class, int.class, long.class,

				new XC_MethodHook() {

					@Override
					protected void beforeHookedMethod(MethodHookParam param)
							throws Throwable {
						int handle = (int) param.args[0];
						if (handle == ALS_HANDLE) {
							float[] values = (float[]) param.args[1];

							float lux = values[0];
							if (Float.compare(lux, BOGUS_LUX_VALUE) == 0) {
								param.setResult(null);
								Log.v("LightSensorFilter",
										"suppresed bogus light sensor value " +
										lux + "lux");
							}
						}

						// invoke the original method
					}

				});
	}

}

@@ -753,173 +753,692 @@
 %09%09%09%09
-new XC_MethodHook() %7B%0A%0A%09%09%09%09%09@Override%0A%09%09%09%09%09protected void beforeHookedMethod(MethodHookParam param)%0A%09%09%09%09%09%09%09throws Throwable %7B%0A%09%09%09%09%09%09int handle = (int) param.args%5B0%5D;
+// dispatchSensorEvent(int handle, float%5B%5D values,%0A%09%09%09%09//         int inAccuracy, long timestamp)%0A%09%09%09%09//   (see /core/java/android/hardware/SystemSensorManager.java)%0A%09%09%09%09// is called from native Receiver::handleEvent(int, int, void *)%0A%09%09%09%09//   (see /core/jni/android_hardware_SensorManager.cpp)%0A%09%09%09%09// each time a new value is read from HAL.%0A%0A%09%09%09%09new XC_MethodHook() %7B%0A%0A%09%09%09%09%09@Override%0A%09%09%09%09%09protected void beforeHookedMethod(MethodHookParam param)%0A%09%09%09%09%09%09%09throws Throwable %7B%0A%09%09%09%09%09%09int handle = (int) param.args%5B0%5D;%0A%0A%09%09%09%09%09%09// We probably could perform more reliable checks%0A%09%09%09%09%09%09// for a matching sensor here, but... who cares?%0A%09%09%09%09%09%09// On Hammerhead light sensor seems to have handle 1.
 %0A%09%09%09

Add a comment with references to the original methods being hooked