1 /* 2 * Copyright (C) 2025 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.content.om; 18 19 import android.annotation.IntDef; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.text.TextUtils; 23 24 import androidx.annotation.NonNull; 25 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 import java.util.List; 29 import java.util.Objects; 30 31 /** 32 * Constraint for enabling a RRO. Currently this can be a displayId or a deviceId, i.e., 33 * the overlay would be applied only when a target package is running on the given displayId 34 * or deviceId. 35 * 36 * @hide 37 */ 38 public final class OverlayConstraint implements Parcelable { 39 40 /** 41 * Constraint type for enabling a RRO for a specific display id. For contexts associated with 42 * the default display, this would be {@link android.view.Display#DEFAULT_DISPLAY}, and 43 * for contexts associated with a virtual display, this would be the id of the virtual display. 44 */ 45 public static final int TYPE_DISPLAY_ID = 0; 46 47 /** 48 * Constraint type for enabling a RRO for a specific device id. For contexts associated with 49 * the default device, this would be {@link android.content.Context#DEVICE_ID_DEFAULT}, and 50 * for contexts associated with virtual device, this would be the id of the virtual device. 51 */ 52 public static final int TYPE_DEVICE_ID = 1; 53 54 @IntDef(prefix = "TYPE_", value = { 55 TYPE_DISPLAY_ID, 56 TYPE_DEVICE_ID, 57 }) 58 @Retention(RetentionPolicy.SOURCE) 59 @interface ConstraintType { 60 } 61 62 @ConstraintType 63 private final int mType; 64 private final int mValue; 65 OverlayConstraint(int type, int value)66 public OverlayConstraint(int type, int value) { 67 if (type != TYPE_DEVICE_ID && type != TYPE_DISPLAY_ID) { 68 throw new IllegalArgumentException( 69 "Type must be either TYPE_DISPLAY_ID or TYPE_DEVICE_ID"); 70 } 71 if (value < 0) { 72 throw new IllegalArgumentException("Value must be greater than 0"); 73 } 74 this.mType = type; 75 this.mValue = value; 76 } 77 OverlayConstraint(Parcel in)78 private OverlayConstraint(Parcel in) { 79 this(in.readInt(), in.readInt()); 80 } 81 82 /** 83 * Returns the type of the constraint. 84 */ getType()85 public int getType() { 86 return mType; 87 } 88 89 /** 90 * Returns the value of the constraint. 91 */ getValue()92 public int getValue() { 93 return mValue; 94 } 95 96 @Override toString()97 public String toString() { 98 return "{type: " + typeToString(mType) + ", value: " + mValue + "}"; 99 } 100 101 @Override equals(Object o)102 public boolean equals(Object o) { 103 if (this == o) return true; 104 if (!(o instanceof OverlayConstraint that)) { 105 return false; 106 } 107 return mType == that.mType && mValue == that.mValue; 108 } 109 110 @Override hashCode()111 public int hashCode() { 112 return Objects.hash(mType, mValue); 113 } 114 115 @Override describeContents()116 public int describeContents() { 117 return 0; 118 } 119 120 @Override writeToParcel(@onNull Parcel dest, int flags)121 public void writeToParcel(@NonNull Parcel dest, int flags) { 122 dest.writeInt(mType); 123 dest.writeInt(mValue); 124 } 125 126 public static final Creator<OverlayConstraint> CREATOR = new Creator<>() { 127 @Override 128 public OverlayConstraint createFromParcel(Parcel in) { 129 return new OverlayConstraint(in); 130 } 131 132 @Override 133 public OverlayConstraint[] newArray(int size) { 134 return new OverlayConstraint[size]; 135 } 136 }; 137 138 /** 139 * Returns a string description for a list of constraints. 140 */ constraintsToString(final List<OverlayConstraint> overlayConstraints)141 public static String constraintsToString(final List<OverlayConstraint> overlayConstraints) { 142 if (overlayConstraints == null || overlayConstraints.isEmpty()) { 143 return "None"; 144 } 145 return "[" + TextUtils.join(",", overlayConstraints) + "]"; 146 } 147 typeToString(@onstraintType int type)148 private static String typeToString(@ConstraintType int type) { 149 return type == TYPE_DEVICE_ID ? "DEVICE_ID" : "DISPLAY_ID"; 150 } 151 } 152