blob: 76c4fb8caa0b952472ae7ae39f49991ab344e0b1 [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) {
Felipe Lemea8d33c22019-03-25 16:36:09 -0700139 return "ContentCaptureOptions [loggingLevel=" + loggingLevel + " (lite)]";
Felipe Leme5001b3b2019-03-20 18:25:28 -0700140 }
Felipe Lemea8d33c22019-03-25 16:36:09 -0700141 final StringBuilder string = new StringBuilder("ContentCaptureOptions [");
142 string.append("loggingLevel=").append(loggingLevel)
143 .append(", maxBufferSize=").append(maxBufferSize)
144 .append(", idleFlushingFrequencyMs=").append(idleFlushingFrequencyMs)
145 .append(", textChangeFlushingFrequencyMs=").append(textChangeFlushingFrequencyMs)
146 .append(", logHistorySize=").append(logHistorySize);
147 if (whitelistedComponents != null) {
148 string.append(", whitelisted=").append(whitelistedComponents);
149 }
150 return string.append(']').toString();
Felipe Leme326f15a2019-02-19 09:42:24 -0800151 }
152
153 /** @hide */
154 public void dumpShort(@NonNull PrintWriter pw) {
155 pw.print("logLvl="); pw.print(loggingLevel);
Felipe Leme5001b3b2019-03-20 18:25:28 -0700156 if (lite) {
157 pw.print(", lite");
158 return;
159 }
Felipe Leme326f15a2019-02-19 09:42:24 -0800160 pw.print(", bufferSize="); pw.print(maxBufferSize);
161 pw.print(", idle="); pw.print(idleFlushingFrequencyMs);
162 pw.print(", textIdle="); pw.print(textChangeFlushingFrequencyMs);
163 pw.print(", logSize="); pw.print(logHistorySize);
164 if (whitelistedComponents != null) {
165 pw.print(", whitelisted="); pw.print(whitelistedComponents);
166 }
167 }
168
169 @Override
170 public int describeContents() {
171 return 0;
172 }
173
174 @Override
175 public void writeToParcel(Parcel parcel, int flags) {
Felipe Leme5001b3b2019-03-20 18:25:28 -0700176 parcel.writeBoolean(lite);
Felipe Leme326f15a2019-02-19 09:42:24 -0800177 parcel.writeInt(loggingLevel);
Felipe Leme5001b3b2019-03-20 18:25:28 -0700178 if (lite) return;
179
Felipe Leme326f15a2019-02-19 09:42:24 -0800180 parcel.writeInt(maxBufferSize);
181 parcel.writeInt(idleFlushingFrequencyMs);
182 parcel.writeInt(textChangeFlushingFrequencyMs);
183 parcel.writeInt(logHistorySize);
184 parcel.writeArraySet(whitelistedComponents);
185 }
186
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700187 public static final @android.annotation.NonNull Parcelable.Creator<ContentCaptureOptions> CREATOR =
Felipe Leme326f15a2019-02-19 09:42:24 -0800188 new Parcelable.Creator<ContentCaptureOptions>() {
189
190 @Override
191 public ContentCaptureOptions createFromParcel(Parcel parcel) {
Felipe Leme5001b3b2019-03-20 18:25:28 -0700192 final boolean lite = parcel.readBoolean();
Felipe Leme326f15a2019-02-19 09:42:24 -0800193 final int loggingLevel = parcel.readInt();
Felipe Leme5001b3b2019-03-20 18:25:28 -0700194 if (lite) {
195 return new ContentCaptureOptions(loggingLevel);
196 }
Felipe Leme326f15a2019-02-19 09:42:24 -0800197 final int maxBufferSize = parcel.readInt();
198 final int idleFlushingFrequencyMs = parcel.readInt();
199 final int textChangeFlushingFrequencyMs = parcel.readInt();
200 final int logHistorySize = parcel.readInt();
201 @SuppressWarnings("unchecked")
202 final ArraySet<ComponentName> whitelistedComponents =
203 (ArraySet<ComponentName>) parcel.readArraySet(null);
204 return new ContentCaptureOptions(loggingLevel, maxBufferSize,
205 idleFlushingFrequencyMs, textChangeFlushingFrequencyMs, logHistorySize,
206 whitelistedComponents);
207 }
208
209 @Override
210 public ContentCaptureOptions[] newArray(int size) {
211 return new ContentCaptureOptions[size];
212 }
Felipe Leme326f15a2019-02-19 09:42:24 -0800213 };
214}