1 /* 2 * Copyright (C) 2019 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 package android.view.contentcapture; 17 18 import android.annotation.IntDef; 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.LocusId; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.util.DebugUtils; 25 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 import java.util.Objects; 29 30 /** 31 * Defines a condition for when content capture should be allowed. 32 * 33 * <p>See {@link ContentCaptureManager#getContentCaptureConditions()} for more. 34 */ 35 public final class ContentCaptureCondition implements Parcelable { 36 37 /** 38 * When set, package should use the {@link LocusId#getId()} as a regular expression (using the 39 * {@link java.util.regex.Pattern} format). 40 */ 41 public static final int FLAG_IS_REGEX = 0x2; 42 43 /** @hide */ 44 @IntDef(prefix = { "FLAG" }, flag = true, value = { 45 FLAG_IS_REGEX 46 }) 47 @Retention(RetentionPolicy.SOURCE) 48 @interface Flags {} 49 50 private final @NonNull LocusId mLocusId; 51 private final @Flags int mFlags; 52 53 /** 54 * Default constructor. 55 * 56 * @param locusId id of the condition, as defined by 57 * {@link ContentCaptureContext#getLocusId()}. 58 * @param flags either {@link ContentCaptureCondition#FLAG_IS_REGEX} (to use a regular 59 * expression match) or {@code 0} (in which case the {@code LocusId} must be an exact match of 60 * the {@code LocusId} used in the {@link ContentCaptureContext}). 61 */ ContentCaptureCondition(@onNull LocusId locusId, @Flags int flags)62 public ContentCaptureCondition(@NonNull LocusId locusId, @Flags int flags) { 63 this.mLocusId = Objects.requireNonNull(locusId); 64 this.mFlags = flags; 65 } 66 67 /** 68 * Gets the {@code LocusId} per se. 69 */ 70 @NonNull getLocusId()71 public LocusId getLocusId() { 72 return mLocusId; 73 } 74 75 /** 76 * Gets the flags associates with this condition. 77 * 78 * @return either {@link ContentCaptureCondition#FLAG_IS_REGEX} or {@code 0}. 79 */ getFlags()80 public @Flags int getFlags() { 81 return mFlags; 82 } 83 84 @Override hashCode()85 public int hashCode() { 86 final int prime = 31; 87 int result = 1; 88 result = prime * result + mFlags; 89 result = prime * result + ((mLocusId == null) ? 0 : mLocusId.hashCode()); 90 return result; 91 } 92 93 @Override equals(@ullable Object obj)94 public boolean equals(@Nullable Object obj) { 95 if (this == obj) return true; 96 if (obj == null) return false; 97 if (getClass() != obj.getClass()) return false; 98 final ContentCaptureCondition other = (ContentCaptureCondition) obj; 99 if (mFlags != other.mFlags) return false; 100 if (mLocusId == null) { 101 if (other.mLocusId != null) return false; 102 } else { 103 if (!mLocusId.equals(other.mLocusId)) return false; 104 } 105 return true; 106 } 107 108 @Override toString()109 public String toString() { 110 final StringBuilder string = new StringBuilder(mLocusId.toString()); // LocusID is PII safe 111 if (mFlags != 0) { 112 string 113 .append(" (") 114 .append(DebugUtils.flagsToString(ContentCaptureCondition.class, "FLAG_", mFlags)) 115 .append(')'); 116 } 117 return string.toString(); 118 } 119 120 @Override describeContents()121 public int describeContents() { 122 return 0; 123 } 124 125 @Override writeToParcel(@onNull Parcel parcel, int flags)126 public void writeToParcel(@NonNull Parcel parcel, int flags) { 127 parcel.writeParcelable(mLocusId, flags); 128 parcel.writeInt(mFlags); 129 } 130 131 public static final @NonNull Parcelable.Creator<ContentCaptureCondition> CREATOR = 132 new Parcelable.Creator<ContentCaptureCondition>() { 133 134 @Override 135 public ContentCaptureCondition createFromParcel(@NonNull Parcel parcel) { 136 return new ContentCaptureCondition(parcel.readParcelable(null, android.content.LocusId.class), 137 parcel.readInt()); 138 } 139 140 @Override 141 public ContentCaptureCondition[] newArray(int size) { 142 return new ContentCaptureCondition[size]; 143 } 144 }; 145 } 146