Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.autofill; |
| 17 | |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 18 | import android.annotation.NonNull; |
Philip P. Moltmann | ebbe2d4 | 2017-07-10 12:07:54 -0700 | [diff] [blame] | 19 | import android.annotation.TestApi; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | import android.view.View; |
| 23 | |
| 24 | /** |
| 25 | * A unique identifier for an autofill node inside an {@link android.app.Activity}. |
| 26 | */ |
| 27 | public final class AutofillId implements Parcelable { |
| 28 | |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 29 | /** @hide */ |
| 30 | public static final int NO_SESSION = 0; |
| 31 | |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 32 | private static final int FLAG_IS_VIRTUAL_INT = 0x1; |
| 33 | private static final int FLAG_IS_VIRTUAL_LONG = 0x2; |
| 34 | private static final int FLAG_HAS_SESSION = 0x4; |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 35 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 36 | private final int mViewId; |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 37 | private final int mFlags; |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 38 | private final int mVirtualIntId; |
| 39 | private final long mVirtualLongId; |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 40 | private final int mSessionId; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 41 | |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 42 | /** @hide */ |
Philip P. Moltmann | ebbe2d4 | 2017-07-10 12:07:54 -0700 | [diff] [blame] | 43 | @TestApi |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 44 | public AutofillId(int id) { |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 45 | this(/* flags= */ 0, id, View.NO_ID, NO_SESSION); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** @hide */ |
Felipe Leme | 67e6209 | 2018-02-15 14:47:31 -0800 | [diff] [blame] | 49 | @TestApi |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 50 | public AutofillId(@NonNull AutofillId parent, int virtualChildId) { |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 51 | this(FLAG_IS_VIRTUAL_INT, parent.mViewId, virtualChildId, NO_SESSION); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** @hide */ |
Felipe Leme | f38d414 | 2019-02-05 09:44:03 -0800 | [diff] [blame] | 55 | @TestApi |
Felipe Leme | 305fd40 | 2018-09-11 18:20:42 +0000 | [diff] [blame] | 56 | public AutofillId(int parentId, int virtualChildId) { |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 57 | this(FLAG_IS_VIRTUAL_INT, parentId, virtualChildId, NO_SESSION); |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** @hide */ |
Felipe Leme | f38d414 | 2019-02-05 09:44:03 -0800 | [diff] [blame] | 61 | @TestApi |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 62 | public AutofillId(@NonNull AutofillId parent, long virtualChildId, int sessionId) { |
| 63 | this(FLAG_IS_VIRTUAL_LONG | FLAG_HAS_SESSION, parent.mViewId, virtualChildId, sessionId); |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 66 | private AutofillId(int flags, int parentId, long virtualChildId, int sessionId) { |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 67 | mFlags = flags; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 68 | mViewId = parentId; |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 69 | mVirtualIntId = ((flags & FLAG_IS_VIRTUAL_INT) != 0) ? (int) virtualChildId : View.NO_ID; |
| 70 | mVirtualLongId = ((flags & FLAG_IS_VIRTUAL_LONG) != 0) ? virtualChildId : View.NO_ID; |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 71 | mSessionId = sessionId; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** @hide */ |
| 75 | public int getViewId() { |
| 76 | return mViewId; |
| 77 | } |
| 78 | |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 79 | /** |
| 80 | * Gets the virtual child id. |
| 81 | * |
| 82 | * <p>Should only be used on subsystems where such id is represented by an {@code int} |
| 83 | * (Assist and Autofill). |
| 84 | * |
| 85 | * @hide |
| 86 | */ |
| 87 | public int getVirtualChildIntId() { |
| 88 | return mVirtualIntId; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 91 | /** |
| 92 | * Gets the virtual child id. |
| 93 | * |
| 94 | * <p>Should only be used on subsystems where such id is represented by a {@code long} |
| 95 | * (ContentCapture). |
| 96 | * |
| 97 | * @hide |
| 98 | */ |
| 99 | public long getVirtualChildLongId() { |
| 100 | return mVirtualLongId; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Checks whether this node represents a virtual child, whose id is represented by an |
| 105 | * {@code int}. |
| 106 | * |
| 107 | * <p>Should only be used on subsystems where such id is represented by an {@code int} |
| 108 | * (Assist and Autofill). |
| 109 | * |
| 110 | * @hide |
| 111 | */ |
| 112 | public boolean isVirtualInt() { |
| 113 | return (mFlags & FLAG_IS_VIRTUAL_INT) != 0; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Checks whether this node represents a virtual child, whose id is represented by an |
| 118 | * {@code long}. |
| 119 | * |
| 120 | * <p>Should only be used on subsystems where such id is represented by a {@code long} |
| 121 | * (ContentCapture). |
| 122 | * |
| 123 | * @hide |
| 124 | */ |
| 125 | public boolean isVirtualLong() { |
| 126 | return (mFlags & FLAG_IS_VIRTUAL_LONG) != 0; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Checks whether this node represents a non-virtual child. |
| 131 | * |
| 132 | * @hide |
| 133 | */ |
| 134 | public boolean isNonVirtual() { |
| 135 | return !isVirtualInt() && !isVirtualLong(); |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | private boolean hasSession() { |
| 139 | return (mFlags & FLAG_HAS_SESSION) != 0; |
| 140 | } |
| 141 | |
| 142 | /** @hide */ |
| 143 | public int getSessionId() { |
| 144 | return mSessionId; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | ///////////////////////////////// |
| 148 | // Object "contract" methods. // |
| 149 | ///////////////////////////////// |
| 150 | |
| 151 | @Override |
| 152 | public int hashCode() { |
| 153 | final int prime = 31; |
| 154 | int result = 1; |
| 155 | result = prime * result + mViewId; |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 156 | result = prime * result + mVirtualIntId; |
| 157 | result = prime * result + (int) (mVirtualLongId ^ (mVirtualLongId >>> 32)); |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 158 | result = prime * result + mSessionId; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 159 | return result; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public boolean equals(Object obj) { |
| 164 | if (this == obj) return true; |
| 165 | if (obj == null) return false; |
| 166 | if (getClass() != obj.getClass()) return false; |
| 167 | final AutofillId other = (AutofillId) obj; |
| 168 | if (mViewId != other.mViewId) return false; |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 169 | if (mVirtualIntId != other.mVirtualIntId) return false; |
| 170 | if (mVirtualLongId != other.mVirtualLongId) return false; |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 171 | if (mSessionId != other.mSessionId) return false; |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 172 | return true; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public String toString() { |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 177 | final StringBuilder builder = new StringBuilder().append(mViewId); |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 178 | if (isVirtualInt()) { |
| 179 | builder.append(':').append(mVirtualIntId); |
| 180 | } else if (isVirtualLong()) { |
| 181 | builder.append(':').append(mVirtualLongId); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 182 | } |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 183 | |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 184 | if (hasSession()) { |
| 185 | builder.append('@').append(mSessionId); |
| 186 | } |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 187 | return builder.toString(); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public int describeContents() { |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void writeToParcel(Parcel parcel, int flags) { |
| 197 | parcel.writeInt(mViewId); |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 198 | parcel.writeInt(mFlags); |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 199 | if (hasSession()) { |
| 200 | parcel.writeInt(mSessionId); |
| 201 | } |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 202 | if (isVirtualInt()) { |
| 203 | parcel.writeInt(mVirtualIntId); |
| 204 | } else if (isVirtualLong()) { |
| 205 | parcel.writeLong(mVirtualLongId); |
| 206 | } |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | public static final Parcelable.Creator<AutofillId> CREATOR = |
| 210 | new Parcelable.Creator<AutofillId>() { |
| 211 | @Override |
| 212 | public AutofillId createFromParcel(Parcel source) { |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 213 | final int viewId = source.readInt(); |
| 214 | final int flags = source.readInt(); |
Felipe Leme | 739b98f | 2019-01-11 12:31:20 -0800 | [diff] [blame] | 215 | final int sessionId = (flags & FLAG_HAS_SESSION) != 0 ? source.readInt() : NO_SESSION; |
Felipe Leme | a75333c | 2019-01-22 15:45:32 -0800 | [diff] [blame] | 216 | if ((flags & FLAG_IS_VIRTUAL_INT) != 0) { |
| 217 | return new AutofillId(flags, viewId, source.readInt(), sessionId); |
| 218 | } |
| 219 | if ((flags & FLAG_IS_VIRTUAL_LONG) != 0) { |
| 220 | return new AutofillId(flags, viewId, source.readLong(), sessionId); |
| 221 | } |
| 222 | return new AutofillId(flags, viewId, View.NO_ID, sessionId); |
Felipe Leme | 640f30a | 2017-03-06 15:44:06 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public AutofillId[] newArray(int size) { |
| 227 | return new AutofillId[size]; |
| 228 | } |
| 229 | }; |
| 230 | } |