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

Commit 893a4d5d authored by Ahmad Khalil's avatar Ahmad Khalil Committed by Android (Google) Code Review
Browse files

Merge "Make Adaptive Haptics Scaling Linear" into main

parents 2f7214b0 5fea2aa6
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -591,9 +591,14 @@ public abstract class VibrationEffect implements Parcelable {
    /**
     * Scale given vibration intensity by the given factor.
     *
     * <p> This scale is not necessarily linear and may apply a gamma correction to the scale
     * factor before using it.
     *
     * @param intensity   relative intensity of the effect, must be between 0 and 1
     * @param scaleFactor scale factor to be applied to the intensity. Values within [0,1) will
     *                    scale down the intensity, values larger than 1 will scale up
     * @return the scaled intensity which will be values within [0, 1].
     *
     * @hide
     */
    public static float scale(float intensity, float scaleFactor) {
@@ -623,6 +628,20 @@ public abstract class VibrationEffect implements Parcelable {
        return MathUtils.constrain(a * fx, 0f, 1f);
    }

    /**
     * Performs a linear scaling on the given vibration intensity by the given factor.
     *
     * @param intensity relative intensity of the effect, must be between 0 and 1.
     * @param scaleFactor scale factor to be applied to the intensity. Values within [0,1) will
     *                    scale down the intensity, values larger than 1 will scale up.
     * @return the scaled intensity which will be values within [0, 1].
     *
     * @hide
     */
    public static float scaleLinearly(float intensity, float scaleFactor) {
        return MathUtils.constrain(intensity * scaleFactor, 0f, 1f);
    }

    /**
     * Returns a compact version of the {@link #toString()} result for debugging purposes.
     *
+8 −0
Original line number Diff line number Diff line
@@ -134,6 +134,14 @@ public final class PrebakedSegment extends VibrationEffectSegment {
        return this;
    }

    /** @hide */
    @NonNull
    @Override
    public PrebakedSegment scaleLinearly(float scaleFactor) {
        // Prebaked effect strength cannot be scaled with this method.
        return this;
    }

    /** @hide */
    @NonNull
    @Override
+18 −2
Original line number Diff line number Diff line
@@ -98,8 +98,24 @@ public final class PrimitiveSegment extends VibrationEffectSegment {
    @NonNull
    @Override
    public PrimitiveSegment scale(float scaleFactor) {
        return new PrimitiveSegment(mPrimitiveId, VibrationEffect.scale(mScale, scaleFactor),
                mDelay);
        float newScale = VibrationEffect.scale(mScale, scaleFactor);
        if (Float.compare(mScale, newScale) == 0) {
            return this;
        }

        return new PrimitiveSegment(mPrimitiveId, newScale, mDelay);
    }

    /** @hide */
    @NonNull
    @Override
    public PrimitiveSegment scaleLinearly(float scaleFactor) {
        float newScale = VibrationEffect.scaleLinearly(mScale, scaleFactor);
        if (Float.compare(mScale, newScale) == 0) {
            return this;
        }

        return new PrimitiveSegment(mPrimitiveId, newScale, mDelay);
    }

    /** @hide */
+15 −0
Original line number Diff line number Diff line
@@ -156,6 +156,21 @@ public final class RampSegment extends VibrationEffectSegment {
                mDuration);
    }

    /** @hide */
    @NonNull
    @Override
    public RampSegment scaleLinearly(float scaleFactor) {
        float newStartAmplitude = VibrationEffect.scaleLinearly(mStartAmplitude, scaleFactor);
        float newEndAmplitude = VibrationEffect.scaleLinearly(mEndAmplitude, scaleFactor);
        if (Float.compare(mStartAmplitude, newStartAmplitude) == 0
                && Float.compare(mEndAmplitude, newEndAmplitude) == 0) {
            return this;
        }
        return new RampSegment(newStartAmplitude, newEndAmplitude, mStartFrequencyHz,
                mEndFrequencyHz,
                mDuration);
    }

    /** @hide */
    @NonNull
    @Override
+19 −2
Original line number Diff line number Diff line
@@ -137,8 +137,25 @@ public final class StepSegment extends VibrationEffectSegment {
        if (Float.compare(mAmplitude, VibrationEffect.DEFAULT_AMPLITUDE) == 0) {
            return this;
        }
        return new StepSegment(VibrationEffect.scale(mAmplitude, scaleFactor), mFrequencyHz,
                mDuration);
        float newAmplitude = VibrationEffect.scale(mAmplitude, scaleFactor);
        if (Float.compare(newAmplitude, mAmplitude) == 0) {
            return this;
        }
        return new StepSegment(newAmplitude, mFrequencyHz, mDuration);
    }

    /** @hide */
    @NonNull
    @Override
    public StepSegment scaleLinearly(float scaleFactor) {
        if (Float.compare(mAmplitude, VibrationEffect.DEFAULT_AMPLITUDE) == 0) {
            return this;
        }
        float newAmplitude = VibrationEffect.scaleLinearly(mAmplitude, scaleFactor);
        if (Float.compare(newAmplitude, mAmplitude) == 0) {
            return this;
        }
        return new StepSegment(newAmplitude, mFrequencyHz, mDuration);
    }

    /** @hide */
Loading