blob: b7cccc66294a5b0760b01a19149215c30a4608c9 [file] [log] [blame]
Dan Egnor95240272009-10-27 18:23:39 -07001/*
2 * Copyright (C) 2009 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
17package android.os;
18
Jeff Sharkeyc36c3b92018-04-19 15:22:45 -060019import static android.Manifest.permission.PACKAGE_USAGE_STATS;
20import static android.Manifest.permission.READ_LOGS;
21
22import android.annotation.Nullable;
23import android.annotation.RequiresPermission;
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -060024import android.annotation.SdkConstant;
25import android.annotation.SdkConstant.SdkConstantType;
Jeff Sharkeyc36c3b92018-04-19 15:22:45 -060026import android.annotation.SystemService;
Andrei Onea24ec3212019-03-15 17:35:05 +000027import android.annotation.UnsupportedAppUsage;
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -070028import android.content.Context;
29import android.util.Log;
30
Dan Egnorf18a01c2009-11-12 11:32:50 -080031import com.android.internal.os.IDropBoxManagerService;
Dan Egnor95240272009-10-27 18:23:39 -070032
33import java.io.ByteArrayInputStream;
Brad Fitzpatrickcc792c42010-10-08 09:50:30 -070034import java.io.Closeable;
Dan Egnor95240272009-10-27 18:23:39 -070035import java.io.File;
Dan Egnor95240272009-10-27 18:23:39 -070036import java.io.IOException;
37import java.io.InputStream;
38import java.util.zip.GZIPInputStream;
39
40/**
41 * Enqueues chunks of data (from various sources -- application crashes, kernel
42 * log records, etc.). The queue is size bounded and will drop old data if the
43 * enqueued data exceeds the maximum size. You can think of this as a
44 * persistent, system-wide, blob-oriented "logcat".
45 *
Dan Egnorf18a01c2009-11-12 11:32:50 -080046 * <p>DropBoxManager entries are not sent anywhere directly, but other system
47 * services and debugging tools may scan and upload entries for processing.
Dan Egnor95240272009-10-27 18:23:39 -070048 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060049@SystemService(Context.DROPBOX_SERVICE)
Dan Egnorf18a01c2009-11-12 11:32:50 -080050public class DropBoxManager {
51 private static final String TAG = "DropBoxManager";
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -070052
53 private final Context mContext;
Andrei Onea24ec3212019-03-15 17:35:05 +000054 @UnsupportedAppUsage
Dan Egnorf18a01c2009-11-12 11:32:50 -080055 private final IDropBoxManagerService mService;
Dan Egnor95240272009-10-27 18:23:39 -070056
57 /** Flag value: Entry's content was deleted to save space. */
58 public static final int IS_EMPTY = 1;
59
60 /** Flag value: Content is human-readable UTF-8 text (can be combined with IS_GZIPPED). */
61 public static final int IS_TEXT = 2;
62
Dan Egnore3cfe2d2009-11-14 16:20:04 -080063 /** Flag value: Content can be decompressed with {@link java.util.zip.GZIPOutputStream}. */
Dan Egnor95240272009-10-27 18:23:39 -070064 public static final int IS_GZIPPED = 4;
65
Dan Egnor6e6d60d2010-07-20 15:24:09 -070066 /** Flag value for serialization only: Value is a byte array, not a file descriptor */
67 private static final int HAS_BYTE_ARRAY = 8;
68
Dan Egnor95240272009-10-27 18:23:39 -070069 /**
Hakan Stillb2475362010-12-07 14:05:55 +010070 * Broadcast Action: This is broadcast when a new entry is added in the dropbox.
71 * You must hold the {@link android.Manifest.permission#READ_LOGS} permission
Michael Wachenschwanzfd6523e2019-03-20 23:16:20 -070072 * in order to receive this broadcast. This broadcast can be rate limited for low priority
73 * entries
Hakan Stillb2475362010-12-07 14:05:55 +010074 *
75 * <p class="note">This is a protected intent that can only be sent
76 * by the system.
77 */
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -060078 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Hakan Stillb2475362010-12-07 14:05:55 +010079 public static final String ACTION_DROPBOX_ENTRY_ADDED =
80 "android.intent.action.DROPBOX_ENTRY_ADDED";
81
82 /**
83 * Extra for {@link android.os.DropBoxManager#ACTION_DROPBOX_ENTRY_ADDED}:
84 * string containing the dropbox tag.
85 */
86 public static final String EXTRA_TAG = "tag";
87
88 /**
89 * Extra for {@link android.os.DropBoxManager#ACTION_DROPBOX_ENTRY_ADDED}:
90 * long integer value containing time (in milliseconds since January 1, 1970 00:00:00 UTC)
91 * when the entry was created.
92 */
93 public static final String EXTRA_TIME = "time";
94
95 /**
Michael Wachenschwanz8b0ee472019-03-21 13:14:14 -070096 * Extra for {@link android.os.DropBoxManager#ACTION_DROPBOX_ENTRY_ADDED}:
97 * integer value containing number of broadcasts dropped due to rate limiting on
98 * this {@link android.os.DropBoxManager#EXTRA_TAG}
99 */
100 public static final String EXTRA_DROPPED_COUNT = "android.os.extra.DROPPED_COUNT";
101
102 /**
Dan Egnor95240272009-10-27 18:23:39 -0700103 * A single entry retrieved from the drop box.
104 * This may include a reference to a stream, so you must call
105 * {@link #close()} when you are done using it.
106 */
Brad Fitzpatrickcc792c42010-10-08 09:50:30 -0700107 public static class Entry implements Parcelable, Closeable {
Dan Egnor95240272009-10-27 18:23:39 -0700108 private final String mTag;
109 private final long mTimeMillis;
110
111 private final byte[] mData;
112 private final ParcelFileDescriptor mFileDescriptor;
113 private final int mFlags;
114
115 /** Create a new empty Entry with no contents. */
116 public Entry(String tag, long millis) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700117 if (tag == null) throw new NullPointerException("tag == null");
118
119 mTag = tag;
120 mTimeMillis = millis;
121 mData = null;
122 mFileDescriptor = null;
123 mFlags = IS_EMPTY;
Dan Egnor95240272009-10-27 18:23:39 -0700124 }
125
126 /** Create a new Entry with plain text contents. */
127 public Entry(String tag, long millis, String text) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700128 if (tag == null) throw new NullPointerException("tag == null");
129 if (text == null) throw new NullPointerException("text == null");
130
131 mTag = tag;
132 mTimeMillis = millis;
133 mData = text.getBytes();
134 mFileDescriptor = null;
135 mFlags = IS_TEXT;
Dan Egnor95240272009-10-27 18:23:39 -0700136 }
137
138 /**
139 * Create a new Entry with byte array contents.
140 * The data array must not be modified after creating this entry.
141 */
142 public Entry(String tag, long millis, byte[] data, int flags) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700143 if (tag == null) throw new NullPointerException("tag == null");
144 if (((flags & IS_EMPTY) != 0) != (data == null)) {
145 throw new IllegalArgumentException("Bad flags: " + flags);
146 }
147
148 mTag = tag;
149 mTimeMillis = millis;
150 mData = data;
151 mFileDescriptor = null;
152 mFlags = flags;
Dan Egnor95240272009-10-27 18:23:39 -0700153 }
154
155 /**
156 * Create a new Entry with streaming data contents.
157 * Takes ownership of the ParcelFileDescriptor.
158 */
159 public Entry(String tag, long millis, ParcelFileDescriptor data, int flags) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700160 if (tag == null) throw new NullPointerException("tag == null");
161 if (((flags & IS_EMPTY) != 0) != (data == null)) {
162 throw new IllegalArgumentException("Bad flags: " + flags);
163 }
164
165 mTag = tag;
166 mTimeMillis = millis;
167 mData = null;
168 mFileDescriptor = data;
169 mFlags = flags;
Dan Egnor95240272009-10-27 18:23:39 -0700170 }
171
172 /**
173 * Create a new Entry with the contents read from a file.
174 * The file will be read when the entry's contents are requested.
175 */
176 public Entry(String tag, long millis, File data, int flags) throws IOException {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700177 if (tag == null) throw new NullPointerException("tag == null");
178 if ((flags & IS_EMPTY) != 0) throw new IllegalArgumentException("Bad flags: " + flags);
Dan Egnor95240272009-10-27 18:23:39 -0700179
180 mTag = tag;
181 mTimeMillis = millis;
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700182 mData = null;
183 mFileDescriptor = ParcelFileDescriptor.open(data, ParcelFileDescriptor.MODE_READ_ONLY);
Dan Egnor95240272009-10-27 18:23:39 -0700184 mFlags = flags;
Dan Egnor95240272009-10-27 18:23:39 -0700185 }
186
187 /** Close the input stream associated with this entry. */
188 public void close() {
189 try { if (mFileDescriptor != null) mFileDescriptor.close(); } catch (IOException e) { }
190 }
191
192 /** @return the tag originally attached to the entry. */
193 public String getTag() { return mTag; }
194
195 /** @return time when the entry was originally created. */
196 public long getTimeMillis() { return mTimeMillis; }
197
Brad Fitzpatrick95173b12010-10-07 10:07:34 -0700198 /** @return flags describing the content returned by {@link #getInputStream()}. */
Dan Egnor95240272009-10-27 18:23:39 -0700199 public int getFlags() { return mFlags & ~IS_GZIPPED; } // getInputStream() decompresses.
200
201 /**
202 * @param maxBytes of string to return (will truncate at this length).
203 * @return the uncompressed text contents of the entry, null if the entry is not text.
204 */
205 public String getText(int maxBytes) {
206 if ((mFlags & IS_TEXT) == 0) return null;
207 if (mData != null) return new String(mData, 0, Math.min(maxBytes, mData.length));
208
209 InputStream is = null;
210 try {
211 is = getInputStream();
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700212 if (is == null) return null;
Dan Egnor95240272009-10-27 18:23:39 -0700213 byte[] buf = new byte[maxBytes];
Christian Lindeberge9f18812010-11-16 09:57:54 +0100214 int readBytes = 0;
215 int n = 0;
216 while (n >= 0 && (readBytes += n) < maxBytes) {
217 n = is.read(buf, readBytes, maxBytes - readBytes);
218 }
219 return new String(buf, 0, readBytes);
Dan Egnor95240272009-10-27 18:23:39 -0700220 } catch (IOException e) {
221 return null;
222 } finally {
223 try { if (is != null) is.close(); } catch (IOException e) {}
224 }
225 }
226
227 /** @return the uncompressed contents of the entry, or null if the contents were lost */
228 public InputStream getInputStream() throws IOException {
229 InputStream is;
230 if (mData != null) {
231 is = new ByteArrayInputStream(mData);
232 } else if (mFileDescriptor != null) {
233 is = new ParcelFileDescriptor.AutoCloseInputStream(mFileDescriptor);
234 } else {
235 return null;
236 }
237 return (mFlags & IS_GZIPPED) != 0 ? new GZIPInputStream(is) : is;
238 }
239
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700240 public static final @android.annotation.NonNull Parcelable.Creator<Entry> CREATOR = new Parcelable.Creator() {
Dan Egnor95240272009-10-27 18:23:39 -0700241 public Entry[] newArray(int size) { return new Entry[size]; }
242 public Entry createFromParcel(Parcel in) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700243 String tag = in.readString();
244 long millis = in.readLong();
245 int flags = in.readInt();
246 if ((flags & HAS_BYTE_ARRAY) != 0) {
247 return new Entry(tag, millis, in.createByteArray(), flags & ~HAS_BYTE_ARRAY);
248 } else {
Tim Kilbournba953232015-06-02 15:57:45 -0700249 ParcelFileDescriptor pfd = ParcelFileDescriptor.CREATOR.createFromParcel(in);
250 return new Entry(tag, millis, pfd, flags);
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700251 }
Dan Egnor95240272009-10-27 18:23:39 -0700252 }
253 };
254
255 public int describeContents() {
256 return mFileDescriptor != null ? Parcelable.CONTENTS_FILE_DESCRIPTOR : 0;
257 }
258
259 public void writeToParcel(Parcel out, int flags) {
260 out.writeString(mTag);
261 out.writeLong(mTimeMillis);
262 if (mFileDescriptor != null) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700263 out.writeInt(mFlags & ~HAS_BYTE_ARRAY); // Clear bit just to be safe
264 mFileDescriptor.writeToParcel(out, flags);
Dan Egnor95240272009-10-27 18:23:39 -0700265 } else {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700266 out.writeInt(mFlags | HAS_BYTE_ARRAY);
267 out.writeByteArray(mData);
Dan Egnor95240272009-10-27 18:23:39 -0700268 }
Dan Egnor95240272009-10-27 18:23:39 -0700269 }
270 }
271
272 /** {@hide} */
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700273 public DropBoxManager(Context context, IDropBoxManagerService service) {
274 mContext = context;
275 mService = service;
276 }
Dan Egnor95240272009-10-27 18:23:39 -0700277
278 /**
279 * Create a dummy instance for testing. All methods will fail unless
280 * overridden with an appropriate mock implementation. To obtain a
281 * functional instance, use {@link android.content.Context#getSystemService}.
282 */
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700283 protected DropBoxManager() {
284 mContext = null;
285 mService = null;
286 }
Dan Egnor95240272009-10-27 18:23:39 -0700287
288 /**
289 * Stores human-readable text. The data may be discarded eventually (or even
290 * immediately) if space is limited, or ignored entirely if the tag has been
291 * blocked (see {@link #isTagEnabled}).
292 *
293 * @param tag describing the type of entry being stored
294 * @param data value to store
295 */
296 public void addText(String tag, String data) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700297 try {
298 mService.add(new Entry(tag, 0, data));
299 } catch (RemoteException e) {
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700300 if (e instanceof TransactionTooLargeException
301 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
302 Log.e(TAG, "App sent too much data, so it was ignored", e);
303 return;
304 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700305 throw e.rethrowFromSystemServer();
306 }
Dan Egnor95240272009-10-27 18:23:39 -0700307 }
308
309 /**
310 * Stores binary data, which may be ignored or discarded as with {@link #addText}.
311 *
312 * @param tag describing the type of entry being stored
313 * @param data value to store
314 * @param flags describing the data
315 */
316 public void addData(String tag, byte[] data, int flags) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700317 if (data == null) throw new NullPointerException("data == null");
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700318 try {
319 mService.add(new Entry(tag, 0, data, flags));
320 } catch (RemoteException e) {
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700321 if (e instanceof TransactionTooLargeException
322 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
323 Log.e(TAG, "App sent too much data, so it was ignored", e);
324 return;
325 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700326 throw e.rethrowFromSystemServer();
327 }
Dan Egnor95240272009-10-27 18:23:39 -0700328 }
329
330 /**
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800331 * Stores the contents of a file, which may be ignored or discarded as with
332 * {@link #addText}.
Dan Egnor95240272009-10-27 18:23:39 -0700333 *
334 * @param tag describing the type of entry being stored
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800335 * @param file to read from
Dan Egnor95240272009-10-27 18:23:39 -0700336 * @param flags describing the data
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800337 * @throws IOException if the file can't be opened
Dan Egnor95240272009-10-27 18:23:39 -0700338 */
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800339 public void addFile(String tag, File file, int flags) throws IOException {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700340 if (file == null) throw new NullPointerException("file == null");
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800341 Entry entry = new Entry(tag, 0, file, flags);
342 try {
Brad Fitzpatrick14418942010-06-14 08:58:26 -0700343 mService.add(entry);
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800344 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700345 throw e.rethrowFromSystemServer();
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800346 } finally {
347 entry.close();
348 }
Dan Egnor95240272009-10-27 18:23:39 -0700349 }
350
351 /**
352 * Checks any blacklists (set in system settings) to see whether a certain
353 * tag is allowed. Entries with disabled tags will be dropped immediately,
354 * so you can save the work of actually constructing and sending the data.
355 *
356 * @param tag that would be used in {@link #addText} or {@link #addFile}
357 * @return whether events with that tag would be accepted
358 */
359 public boolean isTagEnabled(String tag) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700360 try {
361 return mService.isTagEnabled(tag);
362 } catch (RemoteException e) {
363 throw e.rethrowFromSystemServer();
364 }
Dan Egnor95240272009-10-27 18:23:39 -0700365 }
366
367 /**
Brad Fitzpatrick30f5c8f2010-10-07 10:48:20 -0700368 * Gets the next entry from the drop box <em>after</em> the specified time.
Jeff Sharkeyc36c3b92018-04-19 15:22:45 -0600369 * You must always call {@link Entry#close()} on the return value!
Dan Egnor95240272009-10-27 18:23:39 -0700370 *
371 * @param tag of entry to look for, null for all tags
372 * @param msec time of the last entry seen
373 * @return the next entry, or null if there are no more entries
374 */
Jeff Sharkeyc36c3b92018-04-19 15:22:45 -0600375 @RequiresPermission(allOf = { READ_LOGS, PACKAGE_USAGE_STATS })
376 public @Nullable Entry getNextEntry(String tag, long msec) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700377 try {
Jeff Sharkeyc36c3b92018-04-19 15:22:45 -0600378 return mService.getNextEntry(tag, msec, mContext.getOpPackageName());
379 } catch (SecurityException e) {
380 if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) {
381 throw e;
382 } else {
383 Log.w(TAG, e.getMessage());
384 return null;
385 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700386 } catch (RemoteException e) {
387 throw e.rethrowFromSystemServer();
388 }
Dan Egnor95240272009-10-27 18:23:39 -0700389 }
390
391 // TODO: It may be useful to have some sort of notification mechanism
392 // when data is added to the dropbox, for demand-driven readers --
393 // for now readers need to poll the dropbox to find new data.
394}