blob: cb85eef5724fdd57d3c66ef8dc61adccb8627a48 [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 Sharkeyb8e8a912016-03-09 16:27:40 -070019import android.content.Context;
20import android.util.Log;
21
Dan Egnorf18a01c2009-11-12 11:32:50 -080022import com.android.internal.os.IDropBoxManagerService;
Dan Egnor95240272009-10-27 18:23:39 -070023
24import java.io.ByteArrayInputStream;
Brad Fitzpatrickcc792c42010-10-08 09:50:30 -070025import java.io.Closeable;
Dan Egnor95240272009-10-27 18:23:39 -070026import java.io.File;
Dan Egnor95240272009-10-27 18:23:39 -070027import java.io.IOException;
28import java.io.InputStream;
29import java.util.zip.GZIPInputStream;
30
31/**
32 * Enqueues chunks of data (from various sources -- application crashes, kernel
33 * log records, etc.). The queue is size bounded and will drop old data if the
34 * enqueued data exceeds the maximum size. You can think of this as a
35 * persistent, system-wide, blob-oriented "logcat".
36 *
37 * <p>You can obtain an instance of this class by calling
38 * {@link android.content.Context#getSystemService}
39 * with {@link android.content.Context#DROPBOX_SERVICE}.
40 *
Dan Egnorf18a01c2009-11-12 11:32:50 -080041 * <p>DropBoxManager entries are not sent anywhere directly, but other system
42 * services and debugging tools may scan and upload entries for processing.
Dan Egnor95240272009-10-27 18:23:39 -070043 */
Dan Egnorf18a01c2009-11-12 11:32:50 -080044public class DropBoxManager {
45 private static final String TAG = "DropBoxManager";
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -070046
47 private final Context mContext;
Dan Egnorf18a01c2009-11-12 11:32:50 -080048 private final IDropBoxManagerService mService;
Dan Egnor95240272009-10-27 18:23:39 -070049
50 /** Flag value: Entry's content was deleted to save space. */
51 public static final int IS_EMPTY = 1;
52
53 /** Flag value: Content is human-readable UTF-8 text (can be combined with IS_GZIPPED). */
54 public static final int IS_TEXT = 2;
55
Dan Egnore3cfe2d2009-11-14 16:20:04 -080056 /** Flag value: Content can be decompressed with {@link java.util.zip.GZIPOutputStream}. */
Dan Egnor95240272009-10-27 18:23:39 -070057 public static final int IS_GZIPPED = 4;
58
Dan Egnor6e6d60d2010-07-20 15:24:09 -070059 /** Flag value for serialization only: Value is a byte array, not a file descriptor */
60 private static final int HAS_BYTE_ARRAY = 8;
61
Dan Egnor95240272009-10-27 18:23:39 -070062 /**
Hakan Stillb2475362010-12-07 14:05:55 +010063 * Broadcast Action: This is broadcast when a new entry is added in the dropbox.
64 * You must hold the {@link android.Manifest.permission#READ_LOGS} permission
65 * in order to receive this broadcast.
66 *
67 * <p class="note">This is a protected intent that can only be sent
68 * by the system.
69 */
70 public static final String ACTION_DROPBOX_ENTRY_ADDED =
71 "android.intent.action.DROPBOX_ENTRY_ADDED";
72
73 /**
74 * Extra for {@link android.os.DropBoxManager#ACTION_DROPBOX_ENTRY_ADDED}:
75 * string containing the dropbox tag.
76 */
77 public static final String EXTRA_TAG = "tag";
78
79 /**
80 * Extra for {@link android.os.DropBoxManager#ACTION_DROPBOX_ENTRY_ADDED}:
81 * long integer value containing time (in milliseconds since January 1, 1970 00:00:00 UTC)
82 * when the entry was created.
83 */
84 public static final String EXTRA_TIME = "time";
85
86 /**
Dan Egnor95240272009-10-27 18:23:39 -070087 * A single entry retrieved from the drop box.
88 * This may include a reference to a stream, so you must call
89 * {@link #close()} when you are done using it.
90 */
Brad Fitzpatrickcc792c42010-10-08 09:50:30 -070091 public static class Entry implements Parcelable, Closeable {
Dan Egnor95240272009-10-27 18:23:39 -070092 private final String mTag;
93 private final long mTimeMillis;
94
95 private final byte[] mData;
96 private final ParcelFileDescriptor mFileDescriptor;
97 private final int mFlags;
98
99 /** Create a new empty Entry with no contents. */
100 public Entry(String tag, long millis) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700101 if (tag == null) throw new NullPointerException("tag == null");
102
103 mTag = tag;
104 mTimeMillis = millis;
105 mData = null;
106 mFileDescriptor = null;
107 mFlags = IS_EMPTY;
Dan Egnor95240272009-10-27 18:23:39 -0700108 }
109
110 /** Create a new Entry with plain text contents. */
111 public Entry(String tag, long millis, String text) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700112 if (tag == null) throw new NullPointerException("tag == null");
113 if (text == null) throw new NullPointerException("text == null");
114
115 mTag = tag;
116 mTimeMillis = millis;
117 mData = text.getBytes();
118 mFileDescriptor = null;
119 mFlags = IS_TEXT;
Dan Egnor95240272009-10-27 18:23:39 -0700120 }
121
122 /**
123 * Create a new Entry with byte array contents.
124 * The data array must not be modified after creating this entry.
125 */
126 public Entry(String tag, long millis, byte[] data, int flags) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700127 if (tag == null) throw new NullPointerException("tag == null");
128 if (((flags & IS_EMPTY) != 0) != (data == null)) {
129 throw new IllegalArgumentException("Bad flags: " + flags);
130 }
131
132 mTag = tag;
133 mTimeMillis = millis;
134 mData = data;
135 mFileDescriptor = null;
136 mFlags = flags;
Dan Egnor95240272009-10-27 18:23:39 -0700137 }
138
139 /**
140 * Create a new Entry with streaming data contents.
141 * Takes ownership of the ParcelFileDescriptor.
142 */
143 public Entry(String tag, long millis, ParcelFileDescriptor data, int flags) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700144 if (tag == null) throw new NullPointerException("tag == null");
145 if (((flags & IS_EMPTY) != 0) != (data == null)) {
146 throw new IllegalArgumentException("Bad flags: " + flags);
147 }
148
149 mTag = tag;
150 mTimeMillis = millis;
151 mData = null;
152 mFileDescriptor = data;
153 mFlags = flags;
Dan Egnor95240272009-10-27 18:23:39 -0700154 }
155
156 /**
157 * Create a new Entry with the contents read from a file.
158 * The file will be read when the entry's contents are requested.
159 */
160 public Entry(String tag, long millis, File data, int flags) throws IOException {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700161 if (tag == null) throw new NullPointerException("tag == null");
162 if ((flags & IS_EMPTY) != 0) throw new IllegalArgumentException("Bad flags: " + flags);
Dan Egnor95240272009-10-27 18:23:39 -0700163
164 mTag = tag;
165 mTimeMillis = millis;
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700166 mData = null;
167 mFileDescriptor = ParcelFileDescriptor.open(data, ParcelFileDescriptor.MODE_READ_ONLY);
Dan Egnor95240272009-10-27 18:23:39 -0700168 mFlags = flags;
Dan Egnor95240272009-10-27 18:23:39 -0700169 }
170
171 /** Close the input stream associated with this entry. */
172 public void close() {
173 try { if (mFileDescriptor != null) mFileDescriptor.close(); } catch (IOException e) { }
174 }
175
176 /** @return the tag originally attached to the entry. */
177 public String getTag() { return mTag; }
178
179 /** @return time when the entry was originally created. */
180 public long getTimeMillis() { return mTimeMillis; }
181
Brad Fitzpatrick95173b12010-10-07 10:07:34 -0700182 /** @return flags describing the content returned by {@link #getInputStream()}. */
Dan Egnor95240272009-10-27 18:23:39 -0700183 public int getFlags() { return mFlags & ~IS_GZIPPED; } // getInputStream() decompresses.
184
185 /**
186 * @param maxBytes of string to return (will truncate at this length).
187 * @return the uncompressed text contents of the entry, null if the entry is not text.
188 */
189 public String getText(int maxBytes) {
190 if ((mFlags & IS_TEXT) == 0) return null;
191 if (mData != null) return new String(mData, 0, Math.min(maxBytes, mData.length));
192
193 InputStream is = null;
194 try {
195 is = getInputStream();
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700196 if (is == null) return null;
Dan Egnor95240272009-10-27 18:23:39 -0700197 byte[] buf = new byte[maxBytes];
Christian Lindeberge9f18812010-11-16 09:57:54 +0100198 int readBytes = 0;
199 int n = 0;
200 while (n >= 0 && (readBytes += n) < maxBytes) {
201 n = is.read(buf, readBytes, maxBytes - readBytes);
202 }
203 return new String(buf, 0, readBytes);
Dan Egnor95240272009-10-27 18:23:39 -0700204 } catch (IOException e) {
205 return null;
206 } finally {
207 try { if (is != null) is.close(); } catch (IOException e) {}
208 }
209 }
210
211 /** @return the uncompressed contents of the entry, or null if the contents were lost */
212 public InputStream getInputStream() throws IOException {
213 InputStream is;
214 if (mData != null) {
215 is = new ByteArrayInputStream(mData);
216 } else if (mFileDescriptor != null) {
217 is = new ParcelFileDescriptor.AutoCloseInputStream(mFileDescriptor);
218 } else {
219 return null;
220 }
221 return (mFlags & IS_GZIPPED) != 0 ? new GZIPInputStream(is) : is;
222 }
223
224 public static final Parcelable.Creator<Entry> CREATOR = new Parcelable.Creator() {
225 public Entry[] newArray(int size) { return new Entry[size]; }
226 public Entry createFromParcel(Parcel in) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700227 String tag = in.readString();
228 long millis = in.readLong();
229 int flags = in.readInt();
230 if ((flags & HAS_BYTE_ARRAY) != 0) {
231 return new Entry(tag, millis, in.createByteArray(), flags & ~HAS_BYTE_ARRAY);
232 } else {
Tim Kilbournba953232015-06-02 15:57:45 -0700233 ParcelFileDescriptor pfd = ParcelFileDescriptor.CREATOR.createFromParcel(in);
234 return new Entry(tag, millis, pfd, flags);
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700235 }
Dan Egnor95240272009-10-27 18:23:39 -0700236 }
237 };
238
239 public int describeContents() {
240 return mFileDescriptor != null ? Parcelable.CONTENTS_FILE_DESCRIPTOR : 0;
241 }
242
243 public void writeToParcel(Parcel out, int flags) {
244 out.writeString(mTag);
245 out.writeLong(mTimeMillis);
246 if (mFileDescriptor != null) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700247 out.writeInt(mFlags & ~HAS_BYTE_ARRAY); // Clear bit just to be safe
248 mFileDescriptor.writeToParcel(out, flags);
Dan Egnor95240272009-10-27 18:23:39 -0700249 } else {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700250 out.writeInt(mFlags | HAS_BYTE_ARRAY);
251 out.writeByteArray(mData);
Dan Egnor95240272009-10-27 18:23:39 -0700252 }
Dan Egnor95240272009-10-27 18:23:39 -0700253 }
254 }
255
256 /** {@hide} */
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700257 public DropBoxManager(Context context, IDropBoxManagerService service) {
258 mContext = context;
259 mService = service;
260 }
Dan Egnor95240272009-10-27 18:23:39 -0700261
262 /**
263 * Create a dummy instance for testing. All methods will fail unless
264 * overridden with an appropriate mock implementation. To obtain a
265 * functional instance, use {@link android.content.Context#getSystemService}.
266 */
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700267 protected DropBoxManager() {
268 mContext = null;
269 mService = null;
270 }
Dan Egnor95240272009-10-27 18:23:39 -0700271
272 /**
273 * Stores human-readable text. The data may be discarded eventually (or even
274 * immediately) if space is limited, or ignored entirely if the tag has been
275 * blocked (see {@link #isTagEnabled}).
276 *
277 * @param tag describing the type of entry being stored
278 * @param data value to store
279 */
280 public void addText(String tag, String data) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700281 try {
282 mService.add(new Entry(tag, 0, data));
283 } catch (RemoteException e) {
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700284 if (e instanceof TransactionTooLargeException
285 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
286 Log.e(TAG, "App sent too much data, so it was ignored", e);
287 return;
288 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700289 throw e.rethrowFromSystemServer();
290 }
Dan Egnor95240272009-10-27 18:23:39 -0700291 }
292
293 /**
294 * Stores binary data, which may be ignored or discarded as with {@link #addText}.
295 *
296 * @param tag describing the type of entry being stored
297 * @param data value to store
298 * @param flags describing the data
299 */
300 public void addData(String tag, byte[] data, int flags) {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700301 if (data == null) throw new NullPointerException("data == null");
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700302 try {
303 mService.add(new Entry(tag, 0, data, flags));
304 } catch (RemoteException e) {
Jeff Sharkeyb8e8a912016-03-09 16:27:40 -0700305 if (e instanceof TransactionTooLargeException
306 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
307 Log.e(TAG, "App sent too much data, so it was ignored", e);
308 return;
309 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700310 throw e.rethrowFromSystemServer();
311 }
Dan Egnor95240272009-10-27 18:23:39 -0700312 }
313
314 /**
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800315 * Stores the contents of a file, which may be ignored or discarded as with
316 * {@link #addText}.
Dan Egnor95240272009-10-27 18:23:39 -0700317 *
318 * @param tag describing the type of entry being stored
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800319 * @param file to read from
Dan Egnor95240272009-10-27 18:23:39 -0700320 * @param flags describing the data
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800321 * @throws IOException if the file can't be opened
Dan Egnor95240272009-10-27 18:23:39 -0700322 */
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800323 public void addFile(String tag, File file, int flags) throws IOException {
Dan Egnor6e6d60d2010-07-20 15:24:09 -0700324 if (file == null) throw new NullPointerException("file == null");
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800325 Entry entry = new Entry(tag, 0, file, flags);
326 try {
Brad Fitzpatrick14418942010-06-14 08:58:26 -0700327 mService.add(entry);
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800328 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700329 throw e.rethrowFromSystemServer();
Dan Egnoreb7a7d52009-11-25 12:38:00 -0800330 } finally {
331 entry.close();
332 }
Dan Egnor95240272009-10-27 18:23:39 -0700333 }
334
335 /**
336 * Checks any blacklists (set in system settings) to see whether a certain
337 * tag is allowed. Entries with disabled tags will be dropped immediately,
338 * so you can save the work of actually constructing and sending the data.
339 *
340 * @param tag that would be used in {@link #addText} or {@link #addFile}
341 * @return whether events with that tag would be accepted
342 */
343 public boolean isTagEnabled(String tag) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700344 try {
345 return mService.isTagEnabled(tag);
346 } catch (RemoteException e) {
347 throw e.rethrowFromSystemServer();
348 }
Dan Egnor95240272009-10-27 18:23:39 -0700349 }
350
351 /**
Brad Fitzpatrick30f5c8f2010-10-07 10:48:20 -0700352 * Gets the next entry from the drop box <em>after</em> the specified time.
353 * Requires <code>android.permission.READ_LOGS</code>. You must always call
Dan Egnor95240272009-10-27 18:23:39 -0700354 * {@link Entry#close()} on the return value!
355 *
356 * @param tag of entry to look for, null for all tags
357 * @param msec time of the last entry seen
358 * @return the next entry, or null if there are no more entries
359 */
360 public Entry getNextEntry(String tag, long msec) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700361 try {
362 return mService.getNextEntry(tag, msec);
363 } catch (RemoteException e) {
364 throw e.rethrowFromSystemServer();
365 }
Dan Egnor95240272009-10-27 18:23:39 -0700366 }
367
368 // TODO: It may be useful to have some sort of notification mechanism
369 // when data is added to the dropbox, for demand-driven readers --
370 // for now readers need to poll the dropbox to find new data.
371}