blob: 1727d341bd82bf788f0e9e9bf0af35565b99dac4 [file] [log] [blame]
Felipe Leme326f15a2019-02-19 09:42:24 -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;
19import android.annotation.Nullable;
20import android.annotation.TestApi;
21import android.app.ActivityThread;
22import android.os.Parcel;
23import android.os.Parcelable;
24import android.util.ArraySet;
25import android.util.Log;
26import android.view.contentcapture.ContentCaptureManager;
27
28import java.io.PrintWriter;
29
30/**
31 * Content capture options for a given package.
32 *
33 * <p>This object is created by the Content Capture System Service and passed back to the app when
34 * the application is created.
35 *
36 * @hide
37 */
38@TestApi
39public final class ContentCaptureOptions implements Parcelable {
40
41 private static final String TAG = ContentCaptureOptions.class.getSimpleName();
42
43 /**
44 * Logging level for {@code logcat} statements.
45 */
46 public final int loggingLevel;
47
48 /**
49 * Maximum number of events that are buffered before sent to the app.
50 */
51 public final int maxBufferSize;
52
53 /**
54 * Frequency the buffer is flushed if idle.
55 */
56 public final int idleFlushingFrequencyMs;
57
58 /**
59 * Frequency the buffer is flushed if last event is a text change.
60 */
61 public final int textChangeFlushingFrequencyMs;
62
63 /**
64 * Size of events that are logging on {@code dump}.
65 */
66 public final int logHistorySize;
67
68 /**
69 * List of activities explicitly whitelisted for content capture (or {@code null} if whitelisted
70 * for all acitivites in the package).
71 */
72 @Nullable
73 public final ArraySet<ComponentName> whitelistedComponents;
74
Felipe Leme5001b3b2019-03-20 18:25:28 -070075 /**
76 * Used to enable just a small set of APIs so it can used by activities belonging to the
77 * content capture service APK.
78 */
79 public final boolean lite;
80
81 public ContentCaptureOptions(int loggingLevel) {
82 this(/* lite= */ true, loggingLevel, /* maxBufferSize= */ 0,
83 /* idleFlushingFrequencyMs= */ 0, /* textChangeFlushingFrequencyMs= */ 0,
84 /* logHistorySize= */ 0, /* whitelistedComponents= */ null);
85 }
86
Felipe Leme326f15a2019-02-19 09:42:24 -080087 public ContentCaptureOptions(int loggingLevel, int maxBufferSize, int idleFlushingFrequencyMs,
88 int textChangeFlushingFrequencyMs, int logHistorySize,
89 @Nullable ArraySet<ComponentName> whitelistedComponents) {
Felipe Leme5001b3b2019-03-20 18:25:28 -070090 this(/* lite= */ false, loggingLevel, maxBufferSize, idleFlushingFrequencyMs,
91 textChangeFlushingFrequencyMs, logHistorySize, whitelistedComponents);
92 }
93
94 private ContentCaptureOptions(boolean lite, int loggingLevel, int maxBufferSize,
95 int idleFlushingFrequencyMs, int textChangeFlushingFrequencyMs, int logHistorySize,
96 @Nullable ArraySet<ComponentName> whitelistedComponents) {
97 this.lite = lite;
Felipe Leme326f15a2019-02-19 09:42:24 -080098 this.loggingLevel = loggingLevel;
99 this.maxBufferSize = maxBufferSize;
100 this.idleFlushingFrequencyMs = idleFlushingFrequencyMs;
101 this.textChangeFlushingFrequencyMs = textChangeFlushingFrequencyMs;
102 this.logHistorySize = logHistorySize;
103 this.whitelistedComponents = whitelistedComponents;
104 }
105
106 /**
107 * @hide
108 */
109 @TestApi
110 public static ContentCaptureOptions forWhitelistingItself() {
111 final ActivityThread at = ActivityThread.currentActivityThread();
112 if (at == null) {
113 throw new IllegalStateException("No ActivityThread");
114 }
115
116 final String packageName = at.getApplication().getPackageName();
117
118 if (!"android.contentcaptureservice.cts".equals(packageName)) {
119 Log.e(TAG, "forWhitelistingItself(): called by " + packageName);
120 throw new SecurityException("Thou shall not pass!");
121 }
122
123 final ContentCaptureOptions options = new ContentCaptureOptions(
124 ContentCaptureManager.LOGGING_LEVEL_VERBOSE,
125 ContentCaptureManager.DEFAULT_MAX_BUFFER_SIZE,
126 ContentCaptureManager.DEFAULT_IDLE_FLUSHING_FREQUENCY_MS,
127 ContentCaptureManager.DEFAULT_TEXT_CHANGE_FLUSHING_FREQUENCY_MS,
128 ContentCaptureManager.DEFAULT_LOG_HISTORY_SIZE,
129 /* whitelistedComponents= */ null);
130 // Always log, as it's used by test only
131 Log.i(TAG, "forWhitelistingItself(" + packageName + "): " + options);
132
133 return options;
134 }
135
136 @Override
137 public String toString() {
Felipe Leme5001b3b2019-03-20 18:25:28 -0700138 if (lite) {
139 return "ContentCaptureOptions [(lite) loggingLevel=" + loggingLevel + "]";
140 }
Felipe Leme326f15a2019-02-19 09:42:24 -0800141 return "ContentCaptureOptions [loggingLevel=" + loggingLevel + ", maxBufferSize="
142 + maxBufferSize + ", idleFlushingFrequencyMs=" + idleFlushingFrequencyMs
143 + ", textChangeFlushingFrequencyMs=" + textChangeFlushingFrequencyMs
144 + ", logHistorySize=" + logHistorySize + ", whitelistedComponents="
145 + whitelistedComponents + "]";
146 }
147
148 /** @hide */
149 public void dumpShort(@NonNull PrintWriter pw) {
150 pw.print("logLvl="); pw.print(loggingLevel);
Felipe Leme5001b3b2019-03-20 18:25:28 -0700151 if (lite) {
152 pw.print(", lite");
153 return;
154 }
Felipe Leme326f15a2019-02-19 09:42:24 -0800155 pw.print(", bufferSize="); pw.print(maxBufferSize);
156 pw.print(", idle="); pw.print(idleFlushingFrequencyMs);
157 pw.print(", textIdle="); pw.print(textChangeFlushingFrequencyMs);
158 pw.print(", logSize="); pw.print(logHistorySize);
159 if (whitelistedComponents != null) {
160 pw.print(", whitelisted="); pw.print(whitelistedComponents);
161 }
162 }
163
164 @Override
165 public int describeContents() {
166 return 0;
167 }
168
169 @Override
170 public void writeToParcel(Parcel parcel, int flags) {
Felipe Leme5001b3b2019-03-20 18:25:28 -0700171 parcel.writeBoolean(lite);
Felipe Leme326f15a2019-02-19 09:42:24 -0800172 parcel.writeInt(loggingLevel);
Felipe Leme5001b3b2019-03-20 18:25:28 -0700173 if (lite) return;
174
Felipe Leme326f15a2019-02-19 09:42:24 -0800175 parcel.writeInt(maxBufferSize);
176 parcel.writeInt(idleFlushingFrequencyMs);
177 parcel.writeInt(textChangeFlushingFrequencyMs);
178 parcel.writeInt(logHistorySize);
179 parcel.writeArraySet(whitelistedComponents);
180 }
181
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700182 public static final @android.annotation.NonNull Parcelable.Creator<ContentCaptureOptions> CREATOR =
Felipe Leme326f15a2019-02-19 09:42:24 -0800183 new Parcelable.Creator<ContentCaptureOptions>() {
184
185 @Override
186 public ContentCaptureOptions createFromParcel(Parcel parcel) {
Felipe Leme5001b3b2019-03-20 18:25:28 -0700187 final boolean lite = parcel.readBoolean();
Felipe Leme326f15a2019-02-19 09:42:24 -0800188 final int loggingLevel = parcel.readInt();
Felipe Leme5001b3b2019-03-20 18:25:28 -0700189 if (lite) {
190 return new ContentCaptureOptions(loggingLevel);
191 }
Felipe Leme326f15a2019-02-19 09:42:24 -0800192 final int maxBufferSize = parcel.readInt();
193 final int idleFlushingFrequencyMs = parcel.readInt();
194 final int textChangeFlushingFrequencyMs = parcel.readInt();
195 final int logHistorySize = parcel.readInt();
196 @SuppressWarnings("unchecked")
197 final ArraySet<ComponentName> whitelistedComponents =
198 (ArraySet<ComponentName>) parcel.readArraySet(null);
199 return new ContentCaptureOptions(loggingLevel, maxBufferSize,
200 idleFlushingFrequencyMs, textChangeFlushingFrequencyMs, logHistorySize,
201 whitelistedComponents);
202 }
203
204 @Override
205 public ContentCaptureOptions[] newArray(int size) {
206 return new ContentCaptureOptions[size];
207 }
Felipe Leme326f15a2019-02-19 09:42:24 -0800208 };
209}