Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 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; |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 20 | import android.annotation.TestApi; |
| 21 | import android.app.ActivityThread; |
| 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 24 | import android.util.ArraySet; |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | import android.view.autofill.AutofillManager; |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 27 | import android.view.contentcapture.ContentCaptureManager.ContentCaptureClient; |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 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 | */ |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 57 | public boolean augmentedAutofillEnabled; |
| 58 | |
| 59 | /** |
| 60 | * List of whitelisted activities. |
| 61 | */ |
| 62 | @Nullable |
| 63 | public ArraySet<ComponentName> whitelistedActivitiesForAugmentedAutofill; |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 64 | |
| 65 | public AutofillOptions(int loggingLevel, boolean compatModeEnabled) { |
| 66 | this.loggingLevel = loggingLevel; |
| 67 | this.compatModeEnabled = compatModeEnabled; |
| 68 | } |
| 69 | |
| 70 | /** |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 71 | * Returns whether activity is whitelisted for augmented autofill. |
| 72 | */ |
| 73 | public boolean isAugmentedAutofillEnabled(@NonNull Context context) { |
Adam He | 70ebc5a | 2019-03-05 14:20:44 -0800 | [diff] [blame] | 74 | if (!augmentedAutofillEnabled) return false; |
| 75 | |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 76 | final ContentCaptureClient contentCaptureClient = context.getContentCaptureClient(); |
| 77 | if (contentCaptureClient == null) return false; |
| 78 | |
| 79 | final ComponentName component = contentCaptureClient.contentCaptureClientGetComponentName(); |
Adam He | 70ebc5a | 2019-03-05 14:20:44 -0800 | [diff] [blame] | 80 | return whitelistedActivitiesForAugmentedAutofill == null |
| 81 | || whitelistedActivitiesForAugmentedAutofill.contains(component); |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 85 | * @hide |
| 86 | */ |
| 87 | @TestApi |
| 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); |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 103 | options.augmentedAutofillEnabled = true; |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 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 |
| 111 | public String toString() { |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 112 | return "AutofillOptions [loggingLevel=" + loggingLevel + ", compatMode=" + compatModeEnabled |
| 113 | + ", augmentedAutofillEnabled=" + augmentedAutofillEnabled + "]"; |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** @hide */ |
| 117 | public void dumpShort(@NonNull PrintWriter pw) { |
| 118 | pw.print("logLvl="); pw.print(loggingLevel); |
| 119 | pw.print(", compatMode="); pw.print(compatModeEnabled); |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 120 | pw.print(", augmented="); pw.print(augmentedAutofillEnabled); |
| 121 | if (whitelistedActivitiesForAugmentedAutofill != null) { |
| 122 | pw.print(", whitelistedActivitiesForAugmentedAutofill="); |
| 123 | pw.print(whitelistedActivitiesForAugmentedAutofill); |
| 124 | } |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int describeContents() { |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void writeToParcel(Parcel parcel, int flags) { |
| 134 | parcel.writeInt(loggingLevel); |
| 135 | parcel.writeBoolean(compatModeEnabled); |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 136 | parcel.writeBoolean(augmentedAutofillEnabled); |
| 137 | parcel.writeArraySet(whitelistedActivitiesForAugmentedAutofill); |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 140 | public static final @android.annotation.NonNull Parcelable.Creator<AutofillOptions> CREATOR = |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 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); |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 148 | options.augmentedAutofillEnabled = parcel.readBoolean(); |
| 149 | options.whitelistedActivitiesForAugmentedAutofill = |
| 150 | (ArraySet<ComponentName>) parcel.readArraySet(null); |
Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 151 | return options; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public AutofillOptions[] newArray(int size) { |
| 156 | return new AutofillOptions[size]; |
| 157 | } |
| 158 | }; |
| 159 | } |