blob: f59bc9891c86a188fa7c09fd5e09e443b167de16 [file] [log] [blame]
Felipe Lemea4f39cd2019-02-19 15:08:59 -08001/*
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 */
16package android.content;
17
18import android.annotation.NonNull;
Adam He6240eab2019-02-25 13:34:45 -080019import android.annotation.Nullable;
Felipe Lemea4f39cd2019-02-19 15:08:59 -080020import android.annotation.TestApi;
21import android.app.ActivityThread;
22import android.os.Parcel;
23import android.os.Parcelable;
Adam He6240eab2019-02-25 13:34:45 -080024import android.util.ArraySet;
Felipe Lemea4f39cd2019-02-19 15:08:59 -080025import android.util.Log;
26import android.view.autofill.AutofillManager;
Adam He6240eab2019-02-25 13:34:45 -080027import android.view.contentcapture.ContentCaptureManager.ContentCaptureClient;
Felipe Lemea4f39cd2019-02-19 15:08:59 -080028
29import 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
40public 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 He6240eab2019-02-25 13:34:45 -080057 public boolean augmentedAutofillEnabled;
58
59 /**
60 * List of whitelisted activities.
61 */
62 @Nullable
63 public ArraySet<ComponentName> whitelistedActivitiesForAugmentedAutofill;
Felipe Lemea4f39cd2019-02-19 15:08:59 -080064
65 public AutofillOptions(int loggingLevel, boolean compatModeEnabled) {
66 this.loggingLevel = loggingLevel;
67 this.compatModeEnabled = compatModeEnabled;
68 }
69
70 /**
Adam He6240eab2019-02-25 13:34:45 -080071 * Returns whether activity is whitelisted for augmented autofill.
72 */
73 public boolean isAugmentedAutofillEnabled(@NonNull Context context) {
Adam He70ebc5a2019-03-05 14:20:44 -080074 if (!augmentedAutofillEnabled) return false;
75
Adam He6240eab2019-02-25 13:34:45 -080076 final ContentCaptureClient contentCaptureClient = context.getContentCaptureClient();
77 if (contentCaptureClient == null) return false;
78
79 final ComponentName component = contentCaptureClient.contentCaptureClientGetComponentName();
Adam He70ebc5a2019-03-05 14:20:44 -080080 return whitelistedActivitiesForAugmentedAutofill == null
81 || whitelistedActivitiesForAugmentedAutofill.contains(component);
Adam He6240eab2019-02-25 13:34:45 -080082 }
83
84 /**
Felipe Lemea4f39cd2019-02-19 15:08:59 -080085 * @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 He6240eab2019-02-25 13:34:45 -0800103 options.augmentedAutofillEnabled = true;
Felipe Lemea4f39cd2019-02-19 15:08:59 -0800104 // 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 He6240eab2019-02-25 13:34:45 -0800112 return "AutofillOptions [loggingLevel=" + loggingLevel + ", compatMode=" + compatModeEnabled
113 + ", augmentedAutofillEnabled=" + augmentedAutofillEnabled + "]";
Felipe Lemea4f39cd2019-02-19 15:08:59 -0800114 }
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 He6240eab2019-02-25 13:34:45 -0800120 pw.print(", augmented="); pw.print(augmentedAutofillEnabled);
121 if (whitelistedActivitiesForAugmentedAutofill != null) {
122 pw.print(", whitelistedActivitiesForAugmentedAutofill=");
123 pw.print(whitelistedActivitiesForAugmentedAutofill);
124 }
Felipe Lemea4f39cd2019-02-19 15:08:59 -0800125 }
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 He6240eab2019-02-25 13:34:45 -0800136 parcel.writeBoolean(augmentedAutofillEnabled);
137 parcel.writeArraySet(whitelistedActivitiesForAugmentedAutofill);
Felipe Lemea4f39cd2019-02-19 15:08:59 -0800138 }
139
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700140 public static final @android.annotation.NonNull Parcelable.Creator<AutofillOptions> CREATOR =
Felipe Lemea4f39cd2019-02-19 15:08:59 -0800141 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 He6240eab2019-02-25 13:34:45 -0800148 options.augmentedAutofillEnabled = parcel.readBoolean();
149 options.whitelistedActivitiesForAugmentedAutofill =
150 (ArraySet<ComponentName>) parcel.readArraySet(null);
Felipe Lemea4f39cd2019-02-19 15:08:59 -0800151 return options;
152 }
153
154 @Override
155 public AutofillOptions[] newArray(int size) {
156 return new AutofillOptions[size];
157 }
158 };
159}