blob: 5608bb37270103ea219eaa2c03136b9d231f45ad [file] [log] [blame]
Felipe Leme640f30a2017-03-06 15:44:06 -08001/*
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 */
16package android.view.autofill;
17
Felipe Leme739b98f2019-01-11 12:31:20 -080018import android.annotation.NonNull;
Philip P. Moltmannebbe2d42017-07-10 12:07:54 -070019import android.annotation.TestApi;
Felipe Leme640f30a2017-03-06 15:44:06 -080020import android.os.Parcel;
21import android.os.Parcelable;
22import android.view.View;
23
24/**
25 * A unique identifier for an autofill node inside an {@link android.app.Activity}.
26 */
27public final class AutofillId implements Parcelable {
28
Felipe Leme739b98f2019-01-11 12:31:20 -080029 /** @hide */
30 public static final int NO_SESSION = 0;
31
Felipe Lemea75333c2019-01-22 15:45:32 -080032 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 Leme739b98f2019-01-11 12:31:20 -080035
Felipe Leme640f30a2017-03-06 15:44:06 -080036 private final int mViewId;
Felipe Leme739b98f2019-01-11 12:31:20 -080037 private final int mFlags;
Felipe Lemea75333c2019-01-22 15:45:32 -080038 private final int mVirtualIntId;
39 private final long mVirtualLongId;
Felipe Leme739b98f2019-01-11 12:31:20 -080040 private final int mSessionId;
Felipe Leme640f30a2017-03-06 15:44:06 -080041
Felipe Leme640f30a2017-03-06 15:44:06 -080042 /** @hide */
Philip P. Moltmannebbe2d42017-07-10 12:07:54 -070043 @TestApi
Felipe Leme640f30a2017-03-06 15:44:06 -080044 public AutofillId(int id) {
Felipe Leme739b98f2019-01-11 12:31:20 -080045 this(/* flags= */ 0, id, View.NO_ID, NO_SESSION);
Felipe Leme640f30a2017-03-06 15:44:06 -080046 }
47
48 /** @hide */
Felipe Leme67e62092018-02-15 14:47:31 -080049 @TestApi
Felipe Leme739b98f2019-01-11 12:31:20 -080050 public AutofillId(@NonNull AutofillId parent, int virtualChildId) {
Felipe Lemea75333c2019-01-22 15:45:32 -080051 this(FLAG_IS_VIRTUAL_INT, parent.mViewId, virtualChildId, NO_SESSION);
Felipe Leme640f30a2017-03-06 15:44:06 -080052 }
53
54 /** @hide */
Felipe Lemef38d4142019-02-05 09:44:03 -080055 @TestApi
Felipe Leme305fd402018-09-11 18:20:42 +000056 public AutofillId(int parentId, int virtualChildId) {
Felipe Lemea75333c2019-01-22 15:45:32 -080057 this(FLAG_IS_VIRTUAL_INT, parentId, virtualChildId, NO_SESSION);
Felipe Leme739b98f2019-01-11 12:31:20 -080058 }
59
60 /** @hide */
Felipe Lemef38d4142019-02-05 09:44:03 -080061 @TestApi
Felipe Lemea75333c2019-01-22 15:45:32 -080062 public AutofillId(@NonNull AutofillId parent, long virtualChildId, int sessionId) {
63 this(FLAG_IS_VIRTUAL_LONG | FLAG_HAS_SESSION, parent.mViewId, virtualChildId, sessionId);
Felipe Leme739b98f2019-01-11 12:31:20 -080064 }
65
Felipe Lemea75333c2019-01-22 15:45:32 -080066 private AutofillId(int flags, int parentId, long virtualChildId, int sessionId) {
Felipe Leme739b98f2019-01-11 12:31:20 -080067 mFlags = flags;
Felipe Leme640f30a2017-03-06 15:44:06 -080068 mViewId = parentId;
Felipe Lemea75333c2019-01-22 15:45:32 -080069 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 Leme739b98f2019-01-11 12:31:20 -080071 mSessionId = sessionId;
Felipe Leme640f30a2017-03-06 15:44:06 -080072 }
73
74 /** @hide */
75 public int getViewId() {
76 return mViewId;
77 }
78
Felipe Lemea75333c2019-01-22 15:45:32 -080079 /**
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 Leme640f30a2017-03-06 15:44:06 -080089 }
90
Felipe Lemea75333c2019-01-22 15:45:32 -080091 /**
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 Leme739b98f2019-01-11 12:31:20 -0800136 }
137
138 private boolean hasSession() {
139 return (mFlags & FLAG_HAS_SESSION) != 0;
140 }
141
142 /** @hide */
143 public int getSessionId() {
144 return mSessionId;
Felipe Leme640f30a2017-03-06 15:44:06 -0800145 }
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 Lemea75333c2019-01-22 15:45:32 -0800156 result = prime * result + mVirtualIntId;
157 result = prime * result + (int) (mVirtualLongId ^ (mVirtualLongId >>> 32));
Felipe Leme739b98f2019-01-11 12:31:20 -0800158 result = prime * result + mSessionId;
Felipe Leme640f30a2017-03-06 15:44:06 -0800159 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 Lemea75333c2019-01-22 15:45:32 -0800169 if (mVirtualIntId != other.mVirtualIntId) return false;
170 if (mVirtualLongId != other.mVirtualLongId) return false;
Felipe Leme739b98f2019-01-11 12:31:20 -0800171 if (mSessionId != other.mSessionId) return false;
Felipe Leme640f30a2017-03-06 15:44:06 -0800172 return true;
173 }
174
175 @Override
176 public String toString() {
Felipe Leme640f30a2017-03-06 15:44:06 -0800177 final StringBuilder builder = new StringBuilder().append(mViewId);
Felipe Lemea75333c2019-01-22 15:45:32 -0800178 if (isVirtualInt()) {
179 builder.append(':').append(mVirtualIntId);
180 } else if (isVirtualLong()) {
181 builder.append(':').append(mVirtualLongId);
Felipe Leme640f30a2017-03-06 15:44:06 -0800182 }
Felipe Lemea75333c2019-01-22 15:45:32 -0800183
Felipe Leme739b98f2019-01-11 12:31:20 -0800184 if (hasSession()) {
185 builder.append('@').append(mSessionId);
186 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800187 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 Leme739b98f2019-01-11 12:31:20 -0800198 parcel.writeInt(mFlags);
Felipe Leme739b98f2019-01-11 12:31:20 -0800199 if (hasSession()) {
200 parcel.writeInt(mSessionId);
201 }
Felipe Lemea75333c2019-01-22 15:45:32 -0800202 if (isVirtualInt()) {
203 parcel.writeInt(mVirtualIntId);
204 } else if (isVirtualLong()) {
205 parcel.writeLong(mVirtualLongId);
206 }
Felipe Leme640f30a2017-03-06 15:44:06 -0800207 }
208
209 public static final Parcelable.Creator<AutofillId> CREATOR =
210 new Parcelable.Creator<AutofillId>() {
211 @Override
212 public AutofillId createFromParcel(Parcel source) {
Felipe Leme739b98f2019-01-11 12:31:20 -0800213 final int viewId = source.readInt();
214 final int flags = source.readInt();
Felipe Leme739b98f2019-01-11 12:31:20 -0800215 final int sessionId = (flags & FLAG_HAS_SESSION) != 0 ? source.readInt() : NO_SESSION;
Felipe Lemea75333c2019-01-22 15:45:32 -0800216 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 Leme640f30a2017-03-06 15:44:06 -0800223 }
224
225 @Override
226 public AutofillId[] newArray(int size) {
227 return new AutofillId[size];
228 }
229 };
230}