1 /* 2 * Copyright 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.window; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.FloatRange; 21 import android.annotation.IntRange; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import androidx.annotation.NonNull; 26 27 import com.android.window.flags.Flags; 28 29 import java.util.Objects; 30 31 /** 32 * Threshold values that are sent with 33 * {@link android.view.WindowManager#registerTrustedPresentationListener(IBinder, 34 * TrustedPresentationThresholds, Executor, Consumer)} 35 */ 36 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) 37 public final class TrustedPresentationThresholds implements Parcelable { 38 /** 39 * The min alpha the Window is required to have to be considered inside the 40 * threshold. 41 */ 42 @FloatRange(from = 0f, fromInclusive = false, to = 1f) 43 private final float mMinAlpha; 44 45 /** 46 * The min fraction of the Window that was presented to the user to be considered 47 * inside the threshold. 48 */ 49 @FloatRange(from = 0f, fromInclusive = false, to = 1f) 50 private final float mMinFractionRendered; 51 52 /** 53 * The time in milliseconds required for the Window to be in the threshold. 54 */ 55 @IntRange(from = 1) 56 private final int mStabilityRequirementMs; 57 58 /** 59 * The min alpha the Window is required to have to be considered inside the 60 * threshold. 61 */ 62 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) getMinAlpha()63 public @FloatRange(from = 0f, fromInclusive = false, to = 1f) float getMinAlpha() { 64 return mMinAlpha; 65 } 66 67 /** 68 * The min fraction of the Window that was presented to the user to be considered 69 * inside the threshold. 70 */ 71 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) getMinFractionRendered()72 public @FloatRange(from = 0f, fromInclusive = false, to = 1f) float getMinFractionRendered() { 73 return mMinFractionRendered; 74 } 75 76 /** 77 * The time in milliseconds required for the Window to be in the threshold. 78 */ 79 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) getStabilityRequirementMillis()80 public @IntRange(from = 1) int getStabilityRequirementMillis() { 81 return mStabilityRequirementMs; 82 } 83 checkValid()84 private void checkValid() { 85 if (mMinAlpha <= 0 || mMinFractionRendered <= 0 || mStabilityRequirementMs < 1) { 86 throw new IllegalArgumentException( 87 "TrustedPresentationThresholds values are invalid"); 88 } 89 } 90 91 /** 92 * Creates a new TrustedPresentationThresholds. 93 * 94 * @param minAlpha The min alpha the Window is required to 95 * have to be considered inside the 96 * threshold. 97 * @param minFractionRendered The min fraction of the Window that was presented 98 * to the user to be considered 99 * inside the threshold. 100 * @param stabilityRequirementMs The time in milliseconds required for the 101 * Window to be in the threshold. 102 */ 103 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) TrustedPresentationThresholds( @loatRangefrom = 0f, fromInclusive = false, to = 1f) float minAlpha, @FloatRange(from = 0f, fromInclusive = false, to = 1f) float minFractionRendered, @IntRange(from = 1) int stabilityRequirementMs)104 public TrustedPresentationThresholds( 105 @FloatRange(from = 0f, fromInclusive = false, to = 1f) float minAlpha, 106 @FloatRange(from = 0f, fromInclusive = false, to = 1f) float minFractionRendered, 107 @IntRange(from = 1) int stabilityRequirementMs) { 108 this.mMinAlpha = minAlpha; 109 this.mMinFractionRendered = minFractionRendered; 110 this.mStabilityRequirementMs = stabilityRequirementMs; 111 checkValid(); 112 } 113 114 @Override 115 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) toString()116 public String toString() { 117 return "TrustedPresentationThresholds { " 118 + "minAlpha = " + mMinAlpha + ", " 119 + "minFractionRendered = " + mMinFractionRendered + ", " 120 + "stabilityRequirementMs = " + mStabilityRequirementMs 121 + " }"; 122 } 123 124 @Override 125 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) writeToParcel(@onNull Parcel dest, int flags)126 public void writeToParcel(@NonNull Parcel dest, int flags) { 127 dest.writeFloat(mMinAlpha); 128 dest.writeFloat(mMinFractionRendered); 129 dest.writeInt(mStabilityRequirementMs); 130 } 131 132 @Override 133 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) describeContents()134 public int describeContents() { 135 return 0; 136 } 137 138 139 @Override 140 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) hashCode()141 public int hashCode() { 142 return Objects.hash(mMinAlpha, mMinFractionRendered, mStabilityRequirementMs); 143 } 144 145 @Override 146 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) equals(Object o)147 public boolean equals(Object o) { 148 if (this == o) { 149 return true; 150 } 151 if (!(o instanceof TrustedPresentationThresholds that)) { 152 return false; 153 } 154 return mMinAlpha == that.mMinAlpha 155 && mMinFractionRendered == that.mMinFractionRendered 156 && mStabilityRequirementMs == that.mStabilityRequirementMs; 157 } 158 159 /** 160 * @hide 161 */ TrustedPresentationThresholds(@onNull Parcel in)162 TrustedPresentationThresholds(@NonNull Parcel in) { 163 mMinAlpha = in.readFloat(); 164 mMinFractionRendered = in.readFloat(); 165 mStabilityRequirementMs = in.readInt(); 166 167 checkValid(); 168 } 169 170 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) 171 public static final @NonNull Creator<TrustedPresentationThresholds> CREATOR = 172 new Creator<TrustedPresentationThresholds>() { 173 @Override 174 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) 175 public TrustedPresentationThresholds[] newArray(int size) { 176 return new TrustedPresentationThresholds[size]; 177 } 178 179 @Override 180 @FlaggedApi(Flags.FLAG_TRUSTED_PRESENTATION_LISTENER_FOR_WINDOW) 181 public TrustedPresentationThresholds createFromParcel(@NonNull Parcel in) { 182 return new TrustedPresentationThresholds(in); 183 } 184 }; 185 } 186