1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.service.autofill;
import static android.view.autofill.Helper.sDebug;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.TestApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Pair;
import android.util.SparseArray;
import android.widget.RemoteViews;
import com.android.internal.util.Preconditions;
import java.util.ArrayList;
/**
* Defines a custom description for the autofill save UI.
*
* <p>This is useful when the autofill service needs to show a detailed view of what would be saved;
* for example, when the screen contains a credit card, it could display a logo of the credit card
* bank, the last four digits of the credit card number, and its expiration number.
*
* <p>A custom description is made of 2 parts:
* <ul>
* <li>A {@link RemoteViews presentation template} containing children views.
* <li>{@link Transformation Transformations} to populate the children views.
* </ul>
*
* <p>For the credit card example mentioned above, the (simplified) template would be:
*
* <pre class="prettyprint">
* <LinearLayout>
* <ImageView android:id="@+id/templateccLogo"/>
* <TextView android:id="@+id/templateCcNumber"/>
* <TextView android:id="@+id/templateExpDate"/>
* </LinearLayout>
* </pre>
*
* <p>Which in code translates to:
*
* <pre class="prettyprint">
* CustomDescription.Builder buider = new Builder(new RemoteViews(pgkName, R.layout.cc_template);
* </pre>
*
* <p>Then the value of each of the 3 children would be changed at runtime based on the the value of
* the screen fields and the {@link Transformation Transformations}:
*
* <pre class="prettyprint">
* // Image child - different logo for each bank, based on credit card prefix
* builder.addChild(R.id.templateccLogo,
* new ImageTransformation.Builder(ccNumberId)
* .addOption(Pattern.compile("^4815.*$"), R.drawable.ic_credit_card_logo1)
* .addOption(Pattern.compile("^1623.*$"), R.drawable.ic_credit_card_logo2)
* .addOption(Pattern.compile("^42.*$"), R.drawable.ic_credit_card_logo3)
* .build();
* // Masked credit card number (as .....LAST_4_DIGITS)
* builder.addChild(R.id.templateCcNumber, new CharSequenceTransformation
* .Builder(ccNumberId, Pattern.compile("^.*(\\d\\d\\d\\d)$"), "...$1")
* .build();
* // Expiration date as MM / YYYY:
* builder.addChild(R.id.templateExpDate, new CharSequenceTransformation
* .Builder(ccExpMonthId, Pattern.compile("^(\\d\\d)$"), "Exp: $1")
* .addField(ccExpYearId, Pattern.compile("^(\\d\\d)$"), "/$1")
* .build();
* </pre>
*
* <p>See {@link ImageTransformation}, {@link CharSequenceTransformation} for more info about these
* transformations.
*/
public final class CustomDescription implements Parcelable {
private final RemoteViews mPresentation;
private final ArrayList<Pair<Integer, InternalTransformation>> mTransformations;
private final ArrayList<Pair<InternalValidator, BatchUpdates>> mUpdates;
private final SparseArray<InternalOnClickAction> mActions;
private CustomDescription(Builder builder) {
mPresentation = builder.mPresentation;
mTransformations = builder.mTransformations;
mUpdates = builder.mUpdates;
mActions = builder.mActions;
}
/** @hide */
@Nullable
public RemoteViews getPresentation() {
return mPresentation;
}
/** @hide */
@Nullable
public ArrayList<Pair<Integer, InternalTransformation>> getTransformations() {
return mTransformations;
}
/** @hide */
@Nullable
public ArrayList<Pair<InternalValidator, BatchUpdates>> getUpdates() {
return mUpdates;
}
/** @hide */
@Nullable
@TestApi
public SparseArray<InternalOnClickAction> getActions() {
return mActions;
}
/**
* Builder for {@link CustomDescription} objects.
*/
public static class Builder {
private final RemoteViews mPresentation;
private boolean mDestroyed;
private ArrayList<Pair<Integer, InternalTransformation>> mTransformations;
private ArrayList<Pair<InternalValidator, BatchUpdates>> mUpdates;
private SparseArray<InternalOnClickAction> mActions;
/**
* Default constructor.
*
* <p><b>Note:</b> If any child view of presentation triggers a
* {@link RemoteViews#setOnClickPendingIntent(int, android.app.PendingIntent) pending intent
* on click}, such {@link PendingIntent} must follow the restrictions below, otherwise
* it might not be triggered or the autofill save UI might not be shown when its activity
* is finished:
* <ul>
* <li>It cannot be created with the {@link PendingIntent#FLAG_IMMUTABLE} flag.
* <li>It must be a PendingIntent for an {@link Activity}.
* <li>The activity must call {@link Activity#finish()} when done.
* <li>The activity should not launch other activities.
* </ul>
*
* @param parentPresentation template presentation with (optional) children views.
* @throws NullPointerException if {@code parentPresentation} is null (on Android
* {@link android.os.Build.VERSION_CODES#P} or higher).
*/
public Builder(@NonNull RemoteViews parentPresentation) {
mPresentation = Preconditions.checkNotNull(parentPresentation);
}
/**
* Adds a transformation to replace the value of a child view with the fields in the
* screen.
*
* <p>When multiple transformations are added for the same child view, they will be applied
* in the same order as added.
*
* @param id view id of the children view.
* @param transformation an implementation provided by the Android System.
*
* @return this builder.
*
* @throws IllegalArgumentException if {@code transformation} is not a class provided
* by the Android System.
* @throws IllegalStateException if {@link #build()} was already called.
*/
@NonNull
public Builder addChild(int id, @NonNull Transformation transformation) {
throwIfDestroyed();
Preconditions.checkArgument((transformation instanceof InternalTransformation),
"not provided by Android System: " + transformation);
if (mTransformations == null) {
mTransformations = new ArrayList<>();
}
mTransformations.add(new Pair<>(id, (InternalTransformation) transformation));
return this;
}
/**
* Updates the {@link RemoteViews presentation template} when a condition is satisfied by
* applying a series of remote view operations. This allows dynamic customization of the
* portion of the save UI that is controlled by the autofill service. Such dynamic
* customization is based on the content of target views.
*
* <p>The updates are applied in the sequence they are added, after the
* {@link #addChild(int, Transformation) transformations} are applied to the children
* views.
*
* <p>For example, to make children views visible when fields are not empty:
*
* <pre class="prettyprint">
* RemoteViews template = new RemoteViews(pgkName, R.layout.my_full_template);
*
* Pattern notEmptyPattern = Pattern.compile(".+");
* Validator hasAddress = new RegexValidator(addressAutofillId, notEmptyPattern);
* Validator hasCcNumber = new RegexValidator(ccNumberAutofillId, notEmptyPattern);
*
* RemoteViews addressUpdates = new RemoteViews(pgkName, R.layout.my_full_template)
* addressUpdates.setViewVisibility(R.id.address, View.VISIBLE);
*
* // Make address visible
* BatchUpdates addressBatchUpdates = new BatchUpdates.Builder()
* .updateTemplate(addressUpdates)
* .build();
*
* RemoteViews ccUpdates = new RemoteViews(pgkName, R.layout.my_full_template)
* ccUpdates.setViewVisibility(R.id.cc_number, View.VISIBLE);
*
* // Mask credit card number (as .....LAST_4_DIGITS) and make it visible
* BatchUpdates ccBatchUpdates = new BatchUpdates.Builder()
* .updateTemplate(ccUpdates)
* .transformChild(R.id.templateCcNumber, new CharSequenceTransformation
* .Builder(ccNumberId, Pattern.compile("^.*(\\d\\d\\d\\d)$"), "...$1")
* .build())
* .build();
*
* CustomDescription customDescription = new CustomDescription.Builder(template)
* .batchUpdate(hasAddress, addressBatchUpdates)
* .batchUpdate(hasCcNumber, ccBatchUpdates)
* .build();
* </pre>
*
* <p>Another approach is to add a child first, then apply the transformations. Example:
*
* <pre class="prettyprint">
* RemoteViews template = new RemoteViews(pgkName, R.layout.my_base_template);
*
* RemoteViews addressPresentation = new RemoteViews(pgkName, R.layout.address)
* RemoteViews addressUpdates = new RemoteViews(pgkName, R.layout.my_template)
* addressUpdates.addView(R.id.parentId, addressPresentation);
* BatchUpdates addressBatchUpdates = new BatchUpdates.Builder()
* .updateTemplate(addressUpdates)
* .build();
*
* RemoteViews ccPresentation = new RemoteViews(pgkName, R.layout.cc)
* RemoteViews ccUpdates = new RemoteViews(pgkName, R.layout.my_template)
* ccUpdates.addView(R.id.parentId, ccPresentation);
* BatchUpdates ccBatchUpdates = new BatchUpdates.Builder()
* .updateTemplate(ccUpdates)
* .transformChild(R.id.templateCcNumber, new CharSequenceTransformation
* .Builder(ccNumberId, Pattern.compile("^.*(\\d\\d\\d\\d)$"), "...$1")
* .build())
* .build();
*
* CustomDescription customDescription = new CustomDescription.Builder(template)
* .batchUpdate(hasAddress, addressBatchUpdates)
* .batchUpdate(hasCcNumber, ccBatchUpdates)
* .build();
* </pre>
*
* @param condition condition used to trigger the updates.
* @param updates actions to be applied to the
* {@link #CustomDescription.Builder(RemoteViews) template presentation} when the condition
* is satisfied.
*
* @return this builder
*
* @throws IllegalArgumentException if {@code condition} is not a class provided
* by the Android System.
* @throws IllegalStateException if {@link #build()} was already called.
*/
@NonNull
public Builder batchUpdate(@NonNull Validator condition, @NonNull BatchUpdates updates) {
throwIfDestroyed();
Preconditions.checkArgument((condition instanceof InternalValidator),
"not provided by Android System: " + condition);
Preconditions.checkNotNull(updates);
if (mUpdates == null) {
mUpdates = new ArrayList<>();
}
mUpdates.add(new Pair<>((InternalValidator) condition, updates));
return this;
}
/**
* Sets an action to be applied to the {@link RemoteViews presentation template} when the
* child view with the given {@code id} is clicked.
*
* <p>Typically used when the presentation uses a masked field (like {@code ****}) for
* sensitive fields like passwords or credit cards numbers, but offers a an icon that the
* user can tap to show the value for that field.
*
* <p>Example:
*
* <pre class="prettyprint">
* customDescriptionBuilder
* .addChild(R.id.password_plain, new CharSequenceTransformation
* .Builder(passwordId, Pattern.compile("^(.*)$"), "$1").build())
* .addOnClickAction(R.id.showIcon, new VisibilitySetterAction
* .Builder(R.id.hideIcon, View.VISIBLE)
* .setVisibility(R.id.showIcon, View.GONE)
* .setVisibility(R.id.password_plain, View.VISIBLE)
* .setVisibility(R.id.password_masked, View.GONE)
* .build())
* .addOnClickAction(R.id.hideIcon, new VisibilitySetterAction
* .Builder(R.id.showIcon, View.VISIBLE)
* .setVisibility(R.id.hideIcon, View.GONE)
* .setVisibility(R.id.password_masked, View.VISIBLE)
* .setVisibility(R.id.password_plain, View.GONE)
* .build());
* </pre>
*
* <p><b>Note:</b> Currently only one action can be applied to a child; if this method
* is called multiple times passing the same {@code id}, only the last call will be used.
*
* @param id resource id of the child view.
* @param action action to be performed. Must be an an implementation provided by the
* Android System.
*
* @return this builder
*
* @throws IllegalArgumentException if {@code action} is not a class provided
* by the Android System.
* @throws IllegalStateException if {@link #build()} was already called.
*/
@NonNull
public Builder addOnClickAction(int id, @NonNull OnClickAction action) {
throwIfDestroyed();
Preconditions.checkArgument((action instanceof InternalOnClickAction),
"not provided by Android System: " + action);
if (mActions == null) {
mActions = new SparseArray<InternalOnClickAction>();
}
mActions.put(id, (InternalOnClickAction) action);
return this;
}
/**
* Creates a new {@link CustomDescription} instance.
*/
@NonNull
public CustomDescription build() {
throwIfDestroyed();
mDestroyed = true;
return new CustomDescription(this);
}
private void throwIfDestroyed() {
if (mDestroyed) {
throw new IllegalStateException("Already called #build()");
}
}
}
/////////////////////////////////////
// Object "contract" methods. //
/////////////////////////////////////
@Override
public String toString() {
if (!sDebug) return super.toString();
return new StringBuilder("CustomDescription: [presentation=")
.append(mPresentation)
.append(", transformations=")
.append(mTransformations == null ? "N/A" : mTransformations.size())
.append(", updates=")
.append(mUpdates == null ? "N/A" : mUpdates.size())
.append(", actions=")
.append(mActions == null ? "N/A" : mActions.size())
.append("]").toString();
}
/////////////////////////////////////
// Parcelable "contract" methods. //
/////////////////////////////////////
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(mPresentation, flags);
if (mPresentation == null) return;
if (mTransformations == null) {
dest.writeIntArray(null);
} else {
final int size = mTransformations.size();
final int[] ids = new int[size];
final InternalTransformation[] values = new InternalTransformation[size];
for (int i = 0; i < size; i++) {
final Pair<Integer, InternalTransformation> pair = mTransformations.get(i);
ids[i] = pair.first;
values[i] = pair.second;
}
dest.writeIntArray(ids);
dest.writeParcelableArray(values, flags);
}
if (mUpdates == null) {
dest.writeParcelableArray(null, flags);
} else {
final int size = mUpdates.size();
final InternalValidator[] conditions = new InternalValidator[size];
final BatchUpdates[] updates = new BatchUpdates[size];
for (int i = 0; i < size; i++) {
final Pair<InternalValidator, BatchUpdates> pair = mUpdates.get(i);
conditions[i] = pair.first;
updates[i] = pair.second;
}
dest.writeParcelableArray(conditions, flags);
dest.writeParcelableArray(updates, flags);
}
if (mActions == null) {
dest.writeIntArray(null);
} else {
final int size = mActions.size();
final int[] ids = new int[size];
final InternalOnClickAction[] values = new InternalOnClickAction[size];
for (int i = 0; i < size; i++) {
ids[i] = mActions.keyAt(i);
values[i] = mActions.valueAt(i);
}
dest.writeIntArray(ids);
dest.writeParcelableArray(values, flags);
}
}
public static final @android.annotation.NonNull Parcelable.Creator<CustomDescription> CREATOR =
new Parcelable.Creator<CustomDescription>() {
@Override
public CustomDescription createFromParcel(Parcel parcel) {
// Always go through the builder to ensure the data ingested by
// the system obeys the contract of the builder to avoid attacks
// using specially crafted parcels.
final RemoteViews parentPresentation = parcel.readParcelable(null);
if (parentPresentation == null) return null;
final Builder builder = new Builder(parentPresentation);
final int[] transformationIds = parcel.createIntArray();
if (transformationIds != null) {
final InternalTransformation[] values =
parcel.readParcelableArray(null, InternalTransformation.class);
final int size = transformationIds.length;
for (int i = 0; i < size; i++) {
builder.addChild(transformationIds[i], values[i]);
}
}
final InternalValidator[] conditions =
parcel.readParcelableArray(null, InternalValidator.class);
if (conditions != null) {
final BatchUpdates[] updates = parcel.readParcelableArray(null, BatchUpdates.class);
final int size = conditions.length;
for (int i = 0; i < size; i++) {
builder.batchUpdate(conditions[i], updates[i]);
}
}
final int[] actionIds = parcel.createIntArray();
if (actionIds != null) {
final InternalOnClickAction[] values =
parcel.readParcelableArray(null, InternalOnClickAction.class);
final int size = actionIds.length;
for (int i = 0; i < size; i++) {
builder.addOnClickAction(actionIds[i], values[i]);
}
}
return builder.build();
}
@Override
public CustomDescription[] newArray(int size) {
return new CustomDescription[size];
}
};
}
|