Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9cfce017 authored by Dave Mankoff's avatar Dave Mankoff Committed by Android (Google) Code Review
Browse files

Merge "Use alternative prox sensor when specified." into rvc-dev

parents 6d2586f0 55d5d8c7
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -201,6 +201,13 @@
         always-on display) -->
    <string name="doze_brightness_sensor_type" translatable="false"></string>

    <!-- Override value to use for proximity sensor.  -->
    <string name="proximity_sensor_type" translatable="false"></string>

    <!-- If using proximity_sensor_type, specifies a threshold value to distinguish near and
         far break points. A sensor value less than this is considered "near". -->
    <item name="proximity_sensor_threshold" translatable="false" format="float" type="dimen"></item>

    <!-- Doze: pulse parameter - how long does it take to fade in? -->
    <integer name="doze_pulse_duration_in">130</integer>

+37 −18
Original line number Diff line number Diff line
@@ -44,8 +44,7 @@ public class ProximitySensor {

    private final Sensor mSensor;
    private final AsyncSensorManager mSensorManager;
    private final boolean mUsingBrightnessSensor;
    private final float mMaxRange;
    private final float mThreshold;
    private List<ProximitySensorListener> mListeners = new ArrayList<>();
    private String mTag = null;
    @VisibleForTesting ProximityEvent mLastEvent;
@@ -68,20 +67,27 @@ public class ProximitySensor {
    public ProximitySensor(@Main Resources resources,
            AsyncSensorManager sensorManager) {
        mSensorManager = sensorManager;
        Sensor sensor = findBrightnessSensor(resources);

        Sensor sensor = findCustomProxSensor(resources);
        float threshold = 0;
        if (sensor != null) {
            try {
                threshold = getCustomProxThreshold(resources);
            } catch (IllegalStateException e) {
                Log.e(TAG, "Can not load custom proximity sensor.", e);
                sensor = null;
            }
        }
        if (sensor == null) {
            mUsingBrightnessSensor = false;
            sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        } else {
            mUsingBrightnessSensor = true;
            if (sensor != null) {
                threshold = sensor.getMaximumRange();
            }
        mSensor = sensor;
        if (mSensor != null) {
            mMaxRange = mSensor.getMaximumRange();
        } else {
            mMaxRange = 0;
        }

        mThreshold = threshold;

        mSensor = sensor;
    }

    public void setTag(String tag) {
@@ -107,9 +113,15 @@ public class ProximitySensor {
        mPaused = false;
        registerInternal();
    }
    /**
     * Returns a brightness sensor that can be used for proximity purposes.
     */
    private Sensor findCustomProxSensor(Resources resources) {
        String sensorType = resources.getString(R.string.proximity_sensor_type);
        if (sensorType.isEmpty()) {
            return null;
        }

    private Sensor findBrightnessSensor(Resources resources) {
        String sensorType = resources.getString(R.string.doze_brightness_sensor_type);
        List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
        Sensor sensor = null;
        for (Sensor s : sensorList) {
@@ -122,6 +134,17 @@ public class ProximitySensor {
        return sensor;
    }

    /**
     * Returns a threshold value that can be used along with {@link #findCustomProxSensor}
     */
    private float getCustomProxThreshold(Resources resources) {
        try {
            return resources.getFloat(R.dimen.proximity_sensor_threshold);
        } catch (Resources.NotFoundException e) {
            throw new IllegalStateException("R.dimen.proximity_sensor_threshold must be set.");
        }
    }

    /**
     * Returns true if we are registered with the SensorManager.
     */
@@ -157,7 +180,6 @@ public class ProximitySensor {
        if (mRegistered || mPaused || mListeners.isEmpty()) {
            return;
        }
        logDebug("Using brightness sensor? " + mUsingBrightnessSensor);
        logDebug("Registering sensor listener");
        mRegistered = true;
        mSensorManager.registerListener(mSensorEventListener, mSensor, mSensorDelay);
@@ -196,10 +218,7 @@ public class ProximitySensor {
    }

    private void onSensorEvent(SensorEvent event) {
        boolean near = event.values[0] < mMaxRange;
        if (mUsingBrightnessSensor) {
            near = event.values[0] == 0;
        }
        boolean near = event.values[0] < mThreshold;
        mLastEvent = new ProximityEvent(near, event.timestamp);
        alertListeners();
    }
+0 −2
Original line number Diff line number Diff line
@@ -20,11 +20,9 @@ import android.content.res.Resources;

public class FakeProximitySensor extends ProximitySensor {
    private boolean mAvailable;
    private boolean mPaused;

    public FakeProximitySensor(Resources resources, AsyncSensorManager sensorManager) {
        super(resources, sensorManager);

        mAvailable = true;
    }