blob: 2fe9f14c9dc90386a1fd631199955baa8a6e27fd [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
75 public ContentCaptureOptions(int loggingLevel, int maxBufferSize, int idleFlushingFrequencyMs,
76 int textChangeFlushingFrequencyMs, int logHistorySize,
77 @Nullable ArraySet<ComponentName> whitelistedComponents) {
78 this.loggingLevel = loggingLevel;
79 this.maxBufferSize = maxBufferSize;
80 this.idleFlushingFrequencyMs = idleFlushingFrequencyMs;
81 this.textChangeFlushingFrequencyMs = textChangeFlushingFrequencyMs;
82 this.logHistorySize = logHistorySize;
83 this.whitelistedComponents = whitelistedComponents;
84 }
85
86 /**
87 * @hide
88 */
89 @TestApi
90 public static ContentCaptureOptions forWhitelistingItself() {
91 final ActivityThread at = ActivityThread.currentActivityThread();
92 if (at == null) {
93 throw new IllegalStateException("No ActivityThread");
94 }
95
96 final String packageName = at.getApplication().getPackageName();
97
98 if (!"android.contentcaptureservice.cts".equals(packageName)) {
99 Log.e(TAG, "forWhitelistingItself(): called by " + packageName);
100 throw new SecurityException("Thou shall not pass!");
101 }
102
103 final ContentCaptureOptions options = new ContentCaptureOptions(
104 ContentCaptureManager.LOGGING_LEVEL_VERBOSE,
105 ContentCaptureManager.DEFAULT_MAX_BUFFER_SIZE,
106 ContentCaptureManager.DEFAULT_IDLE_FLUSHING_FREQUENCY_MS,
107 ContentCaptureManager.DEFAULT_TEXT_CHANGE_FLUSHING_FREQUENCY_MS,
108 ContentCaptureManager.DEFAULT_LOG_HISTORY_SIZE,
109 /* whitelistedComponents= */ null);
110 // Always log, as it's used by test only
111 Log.i(TAG, "forWhitelistingItself(" + packageName + "): " + options);
112
113 return options;
114 }
115
116 @Override
117 public String toString() {
118 return "ContentCaptureOptions [loggingLevel=" + loggingLevel + ", maxBufferSize="
119 + maxBufferSize + ", idleFlushingFrequencyMs=" + idleFlushingFrequencyMs
120 + ", textChangeFlushingFrequencyMs=" + textChangeFlushingFrequencyMs
121 + ", logHistorySize=" + logHistorySize + ", whitelistedComponents="
122 + whitelistedComponents + "]";
123 }
124
125 /** @hide */
126 public void dumpShort(@NonNull PrintWriter pw) {
127 pw.print("logLvl="); pw.print(loggingLevel);
128 pw.print(", bufferSize="); pw.print(maxBufferSize);
129 pw.print(", idle="); pw.print(idleFlushingFrequencyMs);
130 pw.print(", textIdle="); pw.print(textChangeFlushingFrequencyMs);
131 pw.print(", logSize="); pw.print(logHistorySize);
132 if (whitelistedComponents != null) {
133 pw.print(", whitelisted="); pw.print(whitelistedComponents);
134 }
135 }
136
137 @Override
138 public int describeContents() {
139 return 0;
140 }
141
142 @Override
143 public void writeToParcel(Parcel parcel, int flags) {
144 parcel.writeInt(loggingLevel);
145 parcel.writeInt(maxBufferSize);
146 parcel.writeInt(idleFlushingFrequencyMs);
147 parcel.writeInt(textChangeFlushingFrequencyMs);
148 parcel.writeInt(logHistorySize);
149 parcel.writeArraySet(whitelistedComponents);
150 }
151
152 public static final Parcelable.Creator<ContentCaptureOptions> CREATOR =
153 new Parcelable.Creator<ContentCaptureOptions>() {
154
155 @Override
156 public ContentCaptureOptions createFromParcel(Parcel parcel) {
157 final int loggingLevel = parcel.readInt();
158 final int maxBufferSize = parcel.readInt();
159 final int idleFlushingFrequencyMs = parcel.readInt();
160 final int textChangeFlushingFrequencyMs = parcel.readInt();
161 final int logHistorySize = parcel.readInt();
162 @SuppressWarnings("unchecked")
163 final ArraySet<ComponentName> whitelistedComponents =
164 (ArraySet<ComponentName>) parcel.readArraySet(null);
165 return new ContentCaptureOptions(loggingLevel, maxBufferSize,
166 idleFlushingFrequencyMs, textChangeFlushingFrequencyMs, logHistorySize,
167 whitelistedComponents);
168 }
169
170 @Override
171 public ContentCaptureOptions[] newArray(int size) {
172 return new ContentCaptureOptions[size];
173 }
174
175 };
176}