1 /* 2 * Copyright (C) 2018 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.content; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.annotation.TestApi; 21 import android.app.ActivityThread; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.util.ArraySet; 25 import android.util.Log; 26 import android.view.autofill.AutofillManager; 27 import android.view.autofill.AutofillManager.AutofillClient; 28 29 import java.io.PrintWriter; 30 31 /** 32 * Autofill options for a given package. 33 * 34 * <p>This object is created by the Autofill System Service and passed back to the app when the 35 * application is created. 36 * 37 * @hide 38 */ 39 @TestApi 40 public final class AutofillOptions implements Parcelable { 41 42 private static final String TAG = AutofillOptions.class.getSimpleName(); 43 44 /** 45 * Logging level for {@code logcat} statements. 46 */ 47 public final int loggingLevel; 48 49 /** 50 * Whether compatibility mode is enabled for the package. 51 */ 52 public final boolean compatModeEnabled; 53 54 /** 55 * Whether package is whitelisted for augmented autofill. 56 */ 57 public boolean augmentedAutofillEnabled; 58 59 /** 60 * List of whitelisted activities. 61 */ 62 @Nullable 63 public ArraySet<ComponentName> whitelistedActivitiesForAugmentedAutofill; 64 AutofillOptions(int loggingLevel, boolean compatModeEnabled)65 public AutofillOptions(int loggingLevel, boolean compatModeEnabled) { 66 this.loggingLevel = loggingLevel; 67 this.compatModeEnabled = compatModeEnabled; 68 } 69 70 /** 71 * Returns whether activity is whitelisted for augmented autofill. 72 */ isAugmentedAutofillEnabled(@onNull Context context)73 public boolean isAugmentedAutofillEnabled(@NonNull Context context) { 74 if (!augmentedAutofillEnabled) return false; 75 76 final AutofillClient autofillClient = context.getAutofillClient(); 77 if (autofillClient == null) return false; 78 79 final ComponentName component = autofillClient.autofillClientGetComponentName(); 80 return whitelistedActivitiesForAugmentedAutofill == null 81 || whitelistedActivitiesForAugmentedAutofill.contains(component); 82 } 83 84 /** 85 * @hide 86 */ 87 @TestApi forWhitelistingItself()88 public static AutofillOptions forWhitelistingItself() { 89 final ActivityThread at = ActivityThread.currentActivityThread(); 90 if (at == null) { 91 throw new IllegalStateException("No ActivityThread"); 92 } 93 94 final String packageName = at.getApplication().getPackageName(); 95 96 if (!"android.autofillservice.cts".equals(packageName)) { 97 Log.e(TAG, "forWhitelistingItself(): called by " + packageName); 98 throw new SecurityException("Thou shall not pass!"); 99 } 100 101 final AutofillOptions options = new AutofillOptions( 102 AutofillManager.FLAG_ADD_CLIENT_VERBOSE, /* compatModeAllowed= */ true); 103 options.augmentedAutofillEnabled = true; 104 // Always log, as it's used by test only 105 Log.i(TAG, "forWhitelistingItself(" + packageName + "): " + options); 106 107 return options; 108 } 109 110 @Override toString()111 public String toString() { 112 return "AutofillOptions [loggingLevel=" + loggingLevel + ", compatMode=" + compatModeEnabled 113 + ", augmentedAutofillEnabled=" + augmentedAutofillEnabled + "]"; 114 } 115 116 /** @hide */ dumpShort(@onNull PrintWriter pw)117 public void dumpShort(@NonNull PrintWriter pw) { 118 pw.print("logLvl="); pw.print(loggingLevel); 119 pw.print(", compatMode="); pw.print(compatModeEnabled); 120 pw.print(", augmented="); pw.print(augmentedAutofillEnabled); 121 if (whitelistedActivitiesForAugmentedAutofill != null) { 122 pw.print(", whitelistedActivitiesForAugmentedAutofill="); 123 pw.print(whitelistedActivitiesForAugmentedAutofill); 124 } 125 } 126 127 @Override describeContents()128 public int describeContents() { 129 return 0; 130 } 131 132 @Override writeToParcel(Parcel parcel, int flags)133 public void writeToParcel(Parcel parcel, int flags) { 134 parcel.writeInt(loggingLevel); 135 parcel.writeBoolean(compatModeEnabled); 136 parcel.writeBoolean(augmentedAutofillEnabled); 137 parcel.writeArraySet(whitelistedActivitiesForAugmentedAutofill); 138 } 139 140 public static final @android.annotation.NonNull Parcelable.Creator<AutofillOptions> CREATOR = 141 new Parcelable.Creator<AutofillOptions>() { 142 143 @Override 144 public AutofillOptions createFromParcel(Parcel parcel) { 145 final int loggingLevel = parcel.readInt(); 146 final boolean compatMode = parcel.readBoolean(); 147 final AutofillOptions options = new AutofillOptions(loggingLevel, compatMode); 148 options.augmentedAutofillEnabled = parcel.readBoolean(); 149 options.whitelistedActivitiesForAugmentedAutofill = 150 (ArraySet<ComponentName>) parcel.readArraySet(null); 151 return options; 152 } 153 154 @Override 155 public AutofillOptions[] newArray(int size) { 156 return new AutofillOptions[size]; 157 } 158 }; 159 } 160