| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view.autofill; |
| 18 |
|
| 19 |
import static android.view.View.AUTOFILL_TYPE_DATE; |
| 20 |
import static android.view.View.AUTOFILL_TYPE_LIST; |
| 21 |
import static android.view.View.AUTOFILL_TYPE_TEXT; |
| 22 |
import static android.view.View.AUTOFILL_TYPE_TOGGLE; |
| 23 |
import static android.view.autofill.Helper.sDebug; |
| 24 |
import static android.view.autofill.Helper.sVerbose; |
| 25 |
|
| 26 |
import android.annotation.NonNull; |
| 27 |
import android.annotation.Nullable; |
| 28 |
import android.os.Looper; |
| 29 |
import android.os.Parcel; |
| 30 |
import android.os.Parcelable; |
| 31 |
import android.text.TextUtils; |
| 32 |
import android.util.Log; |
| 33 |
import android.view.View; |
| 34 |
|
| 35 |
import com.android.internal.util.Preconditions; |
| 36 |
|
| 37 |
import java.util.Objects; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public final class AutofillValue implements Parcelable { |
| 47 |
|
| 48 |
private static final String TAG = "AutofillValue"; |
| 49 |
|
| 50 |
private final @View.AutofillType int mType; |
| 51 |
private final @NonNull Object mValue; |
| 52 |
|
| 53 |
private AutofillValue(@View.AutofillType int type, @NonNull Object value) { |
| 54 |
mType = type; |
| 55 |
mValue = value; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
@NonNull public CharSequence getTextValue() { |
| 66 |
Preconditions.checkState(isText(), "value must be a text value, not type=%d", mType); |
| 67 |
return (CharSequence) mValue; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public boolean isText() { |
| 76 |
return mType == AUTOFILL_TYPE_TEXT; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public boolean getToggleValue() { |
| 87 |
Preconditions.checkState(isToggle(), "value must be a toggle value, not type=%d", mType); |
| 88 |
return (Boolean) mValue; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public boolean isToggle() { |
| 97 |
return mType == AUTOFILL_TYPE_TOGGLE; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public int getListValue() { |
| 108 |
Preconditions.checkState(isList(), "value must be a list value, not type=%d", mType); |
| 109 |
return (Integer) mValue; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public boolean isList() { |
| 118 |
return mType == AUTOFILL_TYPE_LIST; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public long getDateValue() { |
| 129 |
Preconditions.checkState(isDate(), "value must be a date value, not type=%d", mType); |
| 130 |
return (Long) mValue; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public boolean isDate() { |
| 139 |
return mType == AUTOFILL_TYPE_DATE; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public boolean isEmpty() { |
| 150 |
return isText() && ((CharSequence) mValue).length() == 0; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
@Override |
| 158 |
public int hashCode() { |
| 159 |
return mType + mValue.hashCode(); |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public boolean equals(@Nullable Object obj) { |
| 164 |
if (this == obj) return true; |
| 165 |
if (obj == null) return false; |
| 166 |
if (getClass() != obj.getClass()) return false; |
| 167 |
final AutofillValue other = (AutofillValue) obj; |
| 168 |
|
| 169 |
if (mType != other.mType) return false; |
| 170 |
|
| 171 |
if (isText()) { |
| 172 |
return mValue.toString().equals(other.mValue.toString()); |
| 173 |
} else { |
| 174 |
return Objects.equals(mValue, other.mValue); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public String toString() { |
| 180 |
if (!sDebug) return super.toString(); |
| 181 |
|
| 182 |
final StringBuilder string = new StringBuilder() |
| 183 |
.append("[type=").append(mType) |
| 184 |
.append(", value="); |
| 185 |
if (isText()) { |
| 186 |
Helper.appendRedacted(string, (CharSequence) mValue); |
| 187 |
} else { |
| 188 |
string.append(mValue); |
| 189 |
} |
| 190 |
return string.append(']').toString(); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
@Override |
| 198 |
public int describeContents() { |
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public void writeToParcel(Parcel parcel, int flags) { |
| 204 |
parcel.writeInt(mType); |
| 205 |
|
| 206 |
switch (mType) { |
| 207 |
case AUTOFILL_TYPE_TEXT: |
| 208 |
parcel.writeCharSequence((CharSequence) mValue); |
| 209 |
break; |
| 210 |
case AUTOFILL_TYPE_TOGGLE: |
| 211 |
parcel.writeInt((Boolean) mValue ? 1 : 0); |
| 212 |
break; |
| 213 |
case AUTOFILL_TYPE_LIST: |
| 214 |
parcel.writeInt((Integer) mValue); |
| 215 |
break; |
| 216 |
case AUTOFILL_TYPE_DATE: |
| 217 |
parcel.writeLong((Long) mValue); |
| 218 |
break; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
private AutofillValue(@NonNull Parcel parcel) { |
| 223 |
mType = parcel.readInt(); |
| 224 |
|
| 225 |
switch (mType) { |
| 226 |
case AUTOFILL_TYPE_TEXT: |
| 227 |
mValue = parcel.readCharSequence(); |
| 228 |
break; |
| 229 |
case AUTOFILL_TYPE_TOGGLE: |
| 230 |
int rawValue = parcel.readInt(); |
| 231 |
mValue = rawValue != 0; |
| 232 |
break; |
| 233 |
case AUTOFILL_TYPE_LIST: |
| 234 |
mValue = parcel.readInt(); |
| 235 |
break; |
| 236 |
case AUTOFILL_TYPE_DATE: |
| 237 |
mValue = parcel.readLong(); |
| 238 |
break; |
| 239 |
default: |
| 240 |
throw new IllegalArgumentException("type=" + mType + " not valid"); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
public static final @android.annotation.NonNull Parcelable.Creator<AutofillValue> CREATOR = |
| 245 |
new Parcelable.Creator<AutofillValue>() { |
| 246 |
@Override |
| 247 |
public AutofillValue createFromParcel(Parcel source) { |
| 248 |
return new AutofillValue(source); |
| 249 |
} |
| 250 |
|
| 251 |
@Override |
| 252 |
public AutofillValue[] newArray(int size) { |
| 253 |
return new AutofillValue[size]; |
| 254 |
} |
| 255 |
}; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public static AutofillValue forText(@Nullable CharSequence value) { |
| 270 |
if (sVerbose && !Looper.getMainLooper().isCurrentThread()) { |
| 271 |
Log.v(TAG, "forText() not called on main thread: " + Thread.currentThread()); |
| 272 |
} |
| 273 |
|
| 274 |
return value == null ? null : new AutofillValue(AUTOFILL_TYPE_TEXT, |
| 275 |
TextUtils.trimNoCopySpans(value)); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public static AutofillValue forToggle(boolean value) { |
| 285 |
return new AutofillValue(AUTOFILL_TYPE_TOGGLE, value); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public static AutofillValue forList(int value) { |
| 295 |
return new AutofillValue(AUTOFILL_TYPE_LIST, value); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
public static AutofillValue forDate(long value) { |
| 304 |
return new AutofillValue(AUTOFILL_TYPE_DATE, value); |
| 305 |
} |
| 306 |
} |