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

Commit 13906a2d authored by Vishnu Nair's avatar Vishnu Nair
Browse files

Expose getters instead of internal fields for TrustedPresentationThresholds

Fixes: 316093854
Test: atest TrustedPresentationListenerTest
Change-Id: I8ec96d4c7b4a31aa98e02da862ce0c939c9f0f47
parent 33334554
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -60906,11 +60906,11 @@ package android.window {
  @FlaggedApi("com.android.window.flags.trusted_presentation_listener_for_window") public final class TrustedPresentationThresholds implements android.os.Parcelable {
    ctor @FlaggedApi("com.android.window.flags.trusted_presentation_listener_for_window") public TrustedPresentationThresholds(@FloatRange(from=0.0f, fromInclusive=false, to=1.0f) float, @FloatRange(from=0.0f, fromInclusive=false, to=1.0f) float, @IntRange(from=1) int);
    method @FlaggedApi("com.android.window.flags.trusted_presentation_listener_for_window") public int describeContents();
    method @FlaggedApi("com.android.window.flags.trusted_presentation_listener_for_window") @FloatRange(from=0.0f, fromInclusive=false, to=1.0f) public float getMinAlpha();
    method @FlaggedApi("com.android.window.flags.trusted_presentation_listener_for_window") @FloatRange(from=0.0f, fromInclusive=false, to=1.0f) public float getMinFractionRendered();
    method @FlaggedApi("com.android.window.flags.trusted_presentation_listener_for_window") @IntRange(from=1) public int getStabilityRequirementMillis();
    method @FlaggedApi("com.android.window.flags.trusted_presentation_listener_for_window") public void writeToParcel(@NonNull android.os.Parcel, int);
    field @FlaggedApi("com.android.window.flags.trusted_presentation_listener_for_window") @NonNull public static final android.os.Parcelable.Creator<android.window.TrustedPresentationThresholds> CREATOR;
    field @FlaggedApi("com.android.window.flags.trusted_presentation_listener_for_window") @FloatRange(from=0.0f, fromInclusive=false, to=1.0f) public final float minAlpha;
    field @FlaggedApi("com.android.window.flags.trusted_presentation_listener_for_window") @FloatRange(from=0.0f, fromInclusive=false, to=1.0f) public final float minFractionRendered;
    field @FlaggedApi("com.android.window.flags.trusted_presentation_listener_for_window") @IntRange(from=1) public final int stabilityRequirementMs;
  }
}
+70 −29
Original line number Diff line number Diff line
@@ -19,15 +19,15 @@ package android.window;
import android.annotation.FlaggedApi;
import android.annotation.FloatRange;
import android.annotation.IntRange;
import android.annotation.SuppressLint;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.SurfaceControl;

import androidx.annotation.NonNull;

import com.android.window.flags.Flags;

import java.util.Objects;

/**
 * Threshold values that are sent with
 * {@link android.view.WindowManager#registerTrustedPresentationListener(IBinder,
@@ -36,33 +36,53 @@ import com.android.window.flags.Flags;
@FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW)
public final class TrustedPresentationThresholds implements Parcelable {
    /**
     * The min alpha the {@link SurfaceControl} is required to have to be considered inside the
     * The min alpha the Window is required to have to be considered inside the
     * threshold.
     */
    @FloatRange(from = 0f, fromInclusive = false, to = 1f)
    @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW)
    @SuppressLint("InternalField") // simple data class
    public final float minAlpha;
    private final float mMinAlpha;

    /**
     * The min fraction of the SurfaceControl that was presented to the user to be considered
     * The min fraction of the Window that was presented to the user to be considered
     * inside the threshold.
     */
    @FloatRange(from = 0f, fromInclusive = false, to = 1f)
    @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW)
    @SuppressLint("InternalField") // simple data class
    public final float minFractionRendered;
    private final float mMinFractionRendered;

    /**
     * The time in milliseconds required for the {@link SurfaceControl} to be in the threshold.
     * The time in milliseconds required for the Window to be in the threshold.
     */
    @IntRange(from = 1)
    private final int mStabilityRequirementMs;

    /**
     * The min alpha the Window is required to have to be considered inside the
     * threshold.
     */
    @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW)
    public @FloatRange(from = 0f, fromInclusive = false, to = 1f) float getMinAlpha() {
        return mMinAlpha;
    }

    /**
     * The min fraction of the Window that was presented to the user to be considered
     * inside the threshold.
     */
    @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW)
    public @FloatRange(from = 0f, fromInclusive = false, to = 1f) float getMinFractionRendered() {
        return mMinFractionRendered;
    }

    /**
     * The time in milliseconds required for the Window to be in the threshold.
     */
    @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW)
    @SuppressLint("InternalField") // simple data class
    public final int stabilityRequirementMs;
    public @IntRange(from = 1) int getStabilityRequirementMillis() {
        return mStabilityRequirementMs;
    }

    private void checkValid() {
        if (minAlpha <= 0 || minFractionRendered <= 0 || stabilityRequirementMs < 1) {
        if (mMinAlpha <= 0 || mMinFractionRendered <= 0 || mStabilityRequirementMs < 1) {
            throw new IllegalArgumentException(
                    "TrustedPresentationThresholds values are invalid");
        }
@@ -71,23 +91,23 @@ public final class TrustedPresentationThresholds implements Parcelable {
    /**
     * Creates a new TrustedPresentationThresholds.
     *
     * @param minAlpha               The min alpha the {@link SurfaceControl} is required to
     * @param minAlpha               The min alpha the Window is required to
     *                               have to be considered inside the
     *                               threshold.
     * @param minFractionRendered    The min fraction of the SurfaceControl that was presented
     * @param minFractionRendered    The min fraction of the Window that was presented
     *                               to the user to be considered
     *                               inside the threshold.
     * @param stabilityRequirementMs The time in milliseconds required for the
     *                               {@link SurfaceControl} to be in the threshold.
     *                               Window to be in the threshold.
     */
    @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW)
    public TrustedPresentationThresholds(
            @FloatRange(from = 0f, fromInclusive = false, to = 1f) float minAlpha,
            @FloatRange(from = 0f, fromInclusive = false, to = 1f) float minFractionRendered,
            @IntRange(from = 1) int stabilityRequirementMs) {
        this.minAlpha = minAlpha;
        this.minFractionRendered = minFractionRendered;
        this.stabilityRequirementMs = stabilityRequirementMs;
        this.mMinAlpha = minAlpha;
        this.mMinFractionRendered = minFractionRendered;
        this.mStabilityRequirementMs = stabilityRequirementMs;
        checkValid();
    }

@@ -95,18 +115,18 @@ public final class TrustedPresentationThresholds implements Parcelable {
    @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW)
    public String toString() {
        return "TrustedPresentationThresholds { "
                + "minAlpha = " + minAlpha + ", "
                + "minFractionRendered = " + minFractionRendered + ", "
                + "stabilityRequirementMs = " + stabilityRequirementMs
                + "minAlpha = " + mMinAlpha + ", "
                + "minFractionRendered = " + mMinFractionRendered + ", "
                + "stabilityRequirementMs = " + mStabilityRequirementMs
                + " }";
    }

    @Override
    @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW)
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeFloat(minAlpha);
        dest.writeFloat(minFractionRendered);
        dest.writeInt(stabilityRequirementMs);
        dest.writeFloat(mMinAlpha);
        dest.writeFloat(mMinFractionRendered);
        dest.writeInt(mStabilityRequirementMs);
    }

    @Override
@@ -115,13 +135,34 @@ public final class TrustedPresentationThresholds implements Parcelable {
        return 0;
    }


    @Override
    @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW)
    public int hashCode() {
        return Objects.hash(mMinAlpha, mMinFractionRendered, mStabilityRequirementMs);
    }

    @Override
    @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW)
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof TrustedPresentationThresholds that)) {
            return false;
        }
        return mMinAlpha == that.mMinAlpha
                && mMinFractionRendered == that.mMinFractionRendered
                && mStabilityRequirementMs == that.mStabilityRequirementMs;
    }

    /**
     * @hide
     */
    TrustedPresentationThresholds(@NonNull Parcel in) {
        minAlpha = in.readFloat();
        minFractionRendered = in.readFloat();
        stabilityRequirementMs = in.readInt();
        mMinAlpha = in.readFloat();
        mMinFractionRendered = in.readFloat();
        mStabilityRequirementMs = in.readInt();

        checkValid();
    }
+10 −15
Original line number Diff line number Diff line
@@ -322,15 +322,17 @@ public class TrustedPresentationListenerController {
            var listener = trustedPresentationInfo.mListener;
            boolean lastState = trustedPresentationInfo.mLastComputedTrustedPresentationState;
            boolean newState =
                    (alpha >= trustedPresentationInfo.mThresholds.minAlpha) && (fractionRendered
                            >= trustedPresentationInfo.mThresholds.minFractionRendered);
                    (alpha >= trustedPresentationInfo.mThresholds.getMinAlpha())
                            && (fractionRendered >= trustedPresentationInfo.mThresholds
                                    .getMinFractionRendered());
            trustedPresentationInfo.mLastComputedTrustedPresentationState = newState;

            ProtoLog.v(WM_DEBUG_TPL,
                    "lastState=%s newState=%s alpha=%f minAlpha=%f fractionRendered=%f "
                            + "minFractionRendered=%f",
                    lastState, newState, alpha, trustedPresentationInfo.mThresholds.minAlpha,
                    fractionRendered, trustedPresentationInfo.mThresholds.minFractionRendered);
                    lastState, newState, alpha, trustedPresentationInfo.mThresholds.getMinAlpha(),
                    fractionRendered, trustedPresentationInfo.mThresholds
                            .getMinFractionRendered());

            if (lastState && !newState) {
                // We were in the trusted presentation state, but now we left it,
@@ -350,13 +352,15 @@ public class TrustedPresentationListenerController {
                trustedPresentationInfo.mEnteredTrustedPresentationStateTime = currTimeMs;
                mHandler.postDelayed(() -> {
                    computeTpl(mLastWindowHandles);
                }, (long) (trustedPresentationInfo.mThresholds.stabilityRequirementMs * 1.5));
                }, (long) (trustedPresentationInfo.mThresholds
                            .getStabilityRequirementMillis() * 1.5));
            }

            // Has the timer elapsed, but we are still in the state? Emit a callback if needed
            if (!trustedPresentationInfo.mLastReportedTrustedPresentationState && newState && (
                    currTimeMs - trustedPresentationInfo.mEnteredTrustedPresentationStateTime
                            > trustedPresentationInfo.mThresholds.stabilityRequirementMs)) {
                            > trustedPresentationInfo.mThresholds
                                        .getStabilityRequirementMillis())) {
                trustedPresentationInfo.mLastReportedTrustedPresentationState = true;
                addListenerUpdate(listenerUpdates, listener,
                        trustedPresentationInfo.mId, /*presentationState*/ true);
@@ -413,15 +417,6 @@ public class TrustedPresentationListenerController {
            mThresholds = thresholds;
            mId = id;
            mListener = listener;
            checkValid(thresholds);
        }

        private void checkValid(TrustedPresentationThresholds thresholds) {
            if (thresholds.minAlpha <= 0 || thresholds.minFractionRendered <= 0
                    || thresholds.stabilityRequirementMs < 1) {
                throw new IllegalArgumentException(
                        "TrustedPresentationThresholds values are invalid");
            }
        }
    }
}