1952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor/*
2952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * Copyright (C) 2009 The Android Open Source Project
3952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor *
4952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * Licensed under the Apache License, Version 2.0 (the "License");
5952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * you may not use this file except in compliance with the License.
6952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * You may obtain a copy of the License at
7952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor *
8952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor *      http://www.apache.org/licenses/LICENSE-2.0
9952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor *
10952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * Unless required by applicable law or agreed to in writing, software
11952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * distributed under the License is distributed on an "AS IS" BASIS,
12952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * See the License for the specific language governing permissions and
14952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * limitations under the License.
15952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor */
16952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
17952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnorpackage android.os;
18952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
19f18a01c77e78209b74e34d05cfb352fa4a92db5fDan Egnorimport com.android.internal.os.IDropBoxManagerService;
20952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
21952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnorimport java.io.ByteArrayInputStream;
22cc792c4149b7e768fd894c9b268d815a90a60bd0Brad Fitzpatrickimport java.io.Closeable;
23952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnorimport java.io.File;
24952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnorimport java.io.IOException;
25952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnorimport java.io.InputStream;
26952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnorimport java.util.zip.GZIPInputStream;
27952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
28952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor/**
29952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * Enqueues chunks of data (from various sources -- application crashes, kernel
30952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * log records, etc.).  The queue is size bounded and will drop old data if the
31952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * enqueued data exceeds the maximum size.  You can think of this as a
32952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * persistent, system-wide, blob-oriented "logcat".
33952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor *
34952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * <p>You can obtain an instance of this class by calling
35952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * {@link android.content.Context#getSystemService}
36952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor * with {@link android.content.Context#DROPBOX_SERVICE}.
37952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor *
38f18a01c77e78209b74e34d05cfb352fa4a92db5fDan Egnor * <p>DropBoxManager entries are not sent anywhere directly, but other system
39f18a01c77e78209b74e34d05cfb352fa4a92db5fDan Egnor * services and debugging tools may scan and upload entries for processing.
40952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor */
41f18a01c77e78209b74e34d05cfb352fa4a92db5fDan Egnorpublic class DropBoxManager {
42f18a01c77e78209b74e34d05cfb352fa4a92db5fDan Egnor    private static final String TAG = "DropBoxManager";
43f18a01c77e78209b74e34d05cfb352fa4a92db5fDan Egnor    private final IDropBoxManagerService mService;
44952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
45952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    /** Flag value: Entry's content was deleted to save space. */
46952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    public static final int IS_EMPTY = 1;
47952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
48952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    /** Flag value: Content is human-readable UTF-8 text (can be combined with IS_GZIPPED). */
49952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    public static final int IS_TEXT = 2;
50952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
51e3cfe2d92868513c9524804a362c3c3c8b8cc4e5Dan Egnor    /** Flag value: Content can be decompressed with {@link java.util.zip.GZIPOutputStream}. */
52952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    public static final int IS_GZIPPED = 4;
53952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
546e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor    /** Flag value for serialization only: Value is a byte array, not a file descriptor */
556e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor    private static final int HAS_BYTE_ARRAY = 8;
566e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor
57952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    /**
58b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     * Broadcast Action: This is broadcast when a new entry is added in the dropbox.
59b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     * You must hold the {@link android.Manifest.permission#READ_LOGS} permission
60b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     * in order to receive this broadcast.
61b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     *
62b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     * <p class="note">This is a protected intent that can only be sent
63b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     * by the system.
64b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     */
65b247536aa3d458750edbc6b45b2348a994d83426Hakan Still    public static final String ACTION_DROPBOX_ENTRY_ADDED =
66b247536aa3d458750edbc6b45b2348a994d83426Hakan Still        "android.intent.action.DROPBOX_ENTRY_ADDED";
67b247536aa3d458750edbc6b45b2348a994d83426Hakan Still
68b247536aa3d458750edbc6b45b2348a994d83426Hakan Still    /**
69b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     * Extra for {@link android.os.DropBoxManager#ACTION_DROPBOX_ENTRY_ADDED}:
70b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     * string containing the dropbox tag.
71b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     */
72b247536aa3d458750edbc6b45b2348a994d83426Hakan Still    public static final String EXTRA_TAG = "tag";
73b247536aa3d458750edbc6b45b2348a994d83426Hakan Still
74b247536aa3d458750edbc6b45b2348a994d83426Hakan Still    /**
75b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     * Extra for {@link android.os.DropBoxManager#ACTION_DROPBOX_ENTRY_ADDED}:
76b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     * long integer value containing time (in milliseconds since January 1, 1970 00:00:00 UTC)
77b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     * when the entry was created.
78b247536aa3d458750edbc6b45b2348a994d83426Hakan Still     */
79b247536aa3d458750edbc6b45b2348a994d83426Hakan Still    public static final String EXTRA_TIME = "time";
80b247536aa3d458750edbc6b45b2348a994d83426Hakan Still
81b247536aa3d458750edbc6b45b2348a994d83426Hakan Still    /**
82952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * A single entry retrieved from the drop box.
83952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * This may include a reference to a stream, so you must call
84952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * {@link #close()} when you are done using it.
85952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     */
86cc792c4149b7e768fd894c9b268d815a90a60bd0Brad Fitzpatrick    public static class Entry implements Parcelable, Closeable {
87952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        private final String mTag;
88952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        private final long mTimeMillis;
89952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
90952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        private final byte[] mData;
91952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        private final ParcelFileDescriptor mFileDescriptor;
92952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        private final int mFlags;
93952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
94952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        /** Create a new empty Entry with no contents. */
95952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public Entry(String tag, long millis) {
966e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            if (tag == null) throw new NullPointerException("tag == null");
976e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor
986e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mTag = tag;
996e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mTimeMillis = millis;
1006e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mData = null;
1016e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mFileDescriptor = null;
1026e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mFlags = IS_EMPTY;
103952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        }
104952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
105952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        /** Create a new Entry with plain text contents. */
106952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public Entry(String tag, long millis, String text) {
1076e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            if (tag == null) throw new NullPointerException("tag == null");
1086e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            if (text == null) throw new NullPointerException("text == null");
1096e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor
1106e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mTag = tag;
1116e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mTimeMillis = millis;
1126e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mData = text.getBytes();
1136e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mFileDescriptor = null;
1146e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mFlags = IS_TEXT;
115952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        }
116952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
117952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        /**
118952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         * Create a new Entry with byte array contents.
119952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         * The data array must not be modified after creating this entry.
120952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         */
121952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public Entry(String tag, long millis, byte[] data, int flags) {
1226e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            if (tag == null) throw new NullPointerException("tag == null");
1236e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            if (((flags & IS_EMPTY) != 0) != (data == null)) {
1246e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                throw new IllegalArgumentException("Bad flags: " + flags);
1256e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            }
1266e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor
1276e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mTag = tag;
1286e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mTimeMillis = millis;
1296e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mData = data;
1306e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mFileDescriptor = null;
1316e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mFlags = flags;
132952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        }
133952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
134952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        /**
135952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         * Create a new Entry with streaming data contents.
136952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         * Takes ownership of the ParcelFileDescriptor.
137952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         */
138952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public Entry(String tag, long millis, ParcelFileDescriptor data, int flags) {
1396e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            if (tag == null) throw new NullPointerException("tag == null");
1406e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            if (((flags & IS_EMPTY) != 0) != (data == null)) {
1416e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                throw new IllegalArgumentException("Bad flags: " + flags);
1426e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            }
1436e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor
1446e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mTag = tag;
1456e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mTimeMillis = millis;
1466e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mData = null;
1476e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mFileDescriptor = data;
1486e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mFlags = flags;
149952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        }
150952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
151952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        /**
152952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         * Create a new Entry with the contents read from a file.
153952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         * The file will be read when the entry's contents are requested.
154952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         */
155952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public Entry(String tag, long millis, File data, int flags) throws IOException {
1566e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            if (tag == null) throw new NullPointerException("tag == null");
1576e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            if ((flags & IS_EMPTY) != 0) throw new IllegalArgumentException("Bad flags: " + flags);
158952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
159952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            mTag = tag;
160952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            mTimeMillis = millis;
1616e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mData = null;
1626e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor            mFileDescriptor = ParcelFileDescriptor.open(data, ParcelFileDescriptor.MODE_READ_ONLY);
163952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            mFlags = flags;
164952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        }
165952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
166952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        /** Close the input stream associated with this entry. */
167952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public void close() {
168952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            try { if (mFileDescriptor != null) mFileDescriptor.close(); } catch (IOException e) { }
169952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        }
170952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
171952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        /** @return the tag originally attached to the entry. */
172952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public String getTag() { return mTag; }
173952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
174952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        /** @return time when the entry was originally created. */
175952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public long getTimeMillis() { return mTimeMillis; }
176952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
17795173b14d65cd04ec88d4384b41a80319e432d0bBrad Fitzpatrick        /** @return flags describing the content returned by {@link #getInputStream()}. */
178952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public int getFlags() { return mFlags & ~IS_GZIPPED; }  // getInputStream() decompresses.
179952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
180952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        /**
181952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         * @param maxBytes of string to return (will truncate at this length).
182952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         * @return the uncompressed text contents of the entry, null if the entry is not text.
183952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor         */
184952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public String getText(int maxBytes) {
185952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            if ((mFlags & IS_TEXT) == 0) return null;
186952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            if (mData != null) return new String(mData, 0, Math.min(maxBytes, mData.length));
187952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
188952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            InputStream is = null;
189952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            try {
190952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor                is = getInputStream();
1916e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                if (is == null) return null;
192952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor                byte[] buf = new byte[maxBytes];
193e9f18815218b2ff1f01ea16f2eb0dd17504a9cf3Christian Lindeberg                int readBytes = 0;
194e9f18815218b2ff1f01ea16f2eb0dd17504a9cf3Christian Lindeberg                int n = 0;
195e9f18815218b2ff1f01ea16f2eb0dd17504a9cf3Christian Lindeberg                while (n >= 0 && (readBytes += n) < maxBytes) {
196e9f18815218b2ff1f01ea16f2eb0dd17504a9cf3Christian Lindeberg                    n = is.read(buf, readBytes, maxBytes - readBytes);
197e9f18815218b2ff1f01ea16f2eb0dd17504a9cf3Christian Lindeberg                }
198e9f18815218b2ff1f01ea16f2eb0dd17504a9cf3Christian Lindeberg                return new String(buf, 0, readBytes);
199952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            } catch (IOException e) {
200952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor                return null;
201952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            } finally {
202952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor                try { if (is != null) is.close(); } catch (IOException e) {}
203952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            }
204952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        }
205952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
206952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        /** @return the uncompressed contents of the entry, or null if the contents were lost */
207952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public InputStream getInputStream() throws IOException {
208952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            InputStream is;
209952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            if (mData != null) {
210952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor                is = new ByteArrayInputStream(mData);
211952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            } else if (mFileDescriptor != null) {
212952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor                is = new ParcelFileDescriptor.AutoCloseInputStream(mFileDescriptor);
213952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            } else {
214952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor                return null;
215952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            }
216952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            return (mFlags & IS_GZIPPED) != 0 ? new GZIPInputStream(is) : is;
217952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        }
218952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
219952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public static final Parcelable.Creator<Entry> CREATOR = new Parcelable.Creator() {
220952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            public Entry[] newArray(int size) { return new Entry[size]; }
221952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            public Entry createFromParcel(Parcel in) {
2226e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                String tag = in.readString();
2236e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                long millis = in.readLong();
2246e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                int flags = in.readInt();
2256e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                if ((flags & HAS_BYTE_ARRAY) != 0) {
2266e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                    return new Entry(tag, millis, in.createByteArray(), flags & ~HAS_BYTE_ARRAY);
2276e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                } else {
2284bf27b53b97475803fdb85069d500a075aefc801Tim Kilbourn                    ParcelFileDescriptor pfd = ParcelFileDescriptor.CREATOR.createFromParcel(in);
2294bf27b53b97475803fdb85069d500a075aefc801Tim Kilbourn                    return new Entry(tag, millis, pfd, flags);
2306e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                }
231952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            }
232952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        };
233952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
234952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public int describeContents() {
235952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            return mFileDescriptor != null ? Parcelable.CONTENTS_FILE_DESCRIPTOR : 0;
236952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        }
237952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
238952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        public void writeToParcel(Parcel out, int flags) {
239952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            out.writeString(mTag);
240952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            out.writeLong(mTimeMillis);
241952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            if (mFileDescriptor != null) {
2426e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                out.writeInt(mFlags & ~HAS_BYTE_ARRAY);  // Clear bit just to be safe
2436e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                mFileDescriptor.writeToParcel(out, flags);
244952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            } else {
2456e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                out.writeInt(mFlags | HAS_BYTE_ARRAY);
2466e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor                out.writeByteArray(mData);
247952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor            }
248952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        }
249952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    }
250952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
251952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    /** {@hide} */
252f18a01c77e78209b74e34d05cfb352fa4a92db5fDan Egnor    public DropBoxManager(IDropBoxManagerService service) { mService = service; }
253952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
254952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    /**
255952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * Create a dummy instance for testing.  All methods will fail unless
256952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * overridden with an appropriate mock implementation.  To obtain a
257952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * functional instance, use {@link android.content.Context#getSystemService}.
258952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     */
259f18a01c77e78209b74e34d05cfb352fa4a92db5fDan Egnor    protected DropBoxManager() { mService = null; }
260952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
261952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    /**
262952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * Stores human-readable text.  The data may be discarded eventually (or even
263952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * immediately) if space is limited, or ignored entirely if the tag has been
264952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * blocked (see {@link #isTagEnabled}).
265952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     *
266952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @param tag describing the type of entry being stored
267952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @param data value to store
268952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     */
269952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    public void addText(String tag, String data) {
270952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        try { mService.add(new Entry(tag, 0, data)); } catch (RemoteException e) {}
271952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    }
272952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
273952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    /**
274952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * Stores binary data, which may be ignored or discarded as with {@link #addText}.
275952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     *
276952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @param tag describing the type of entry being stored
277952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @param data value to store
278952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @param flags describing the data
279952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     */
280952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    public void addData(String tag, byte[] data, int flags) {
2816e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor        if (data == null) throw new NullPointerException("data == null");
282952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        try { mService.add(new Entry(tag, 0, data, flags)); } catch (RemoteException e) {}
283952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    }
284952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
285952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    /**
286eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor     * Stores the contents of a file, which may be ignored or discarded as with
287eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor     * {@link #addText}.
288952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     *
289952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @param tag describing the type of entry being stored
290eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor     * @param file to read from
291952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @param flags describing the data
292eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor     * @throws IOException if the file can't be opened
293952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     */
294eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor    public void addFile(String tag, File file, int flags) throws IOException {
2956e6d60d4c85ce440d9ef5e5f36e708ed0ced65c6Dan Egnor        if (file == null) throw new NullPointerException("file == null");
296eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor        Entry entry = new Entry(tag, 0, file, flags);
297eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor        try {
29814418945bdc7a9c2e1cba8deaac5cb6dddfd6412Brad Fitzpatrick            mService.add(entry);
299eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor        } catch (RemoteException e) {
300eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor            // ignore
301eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor        } finally {
302eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor            entry.close();
303eb7a7d57ca50f85b054edadab766b51ff22a2dfdDan Egnor        }
304952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    }
305952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
306952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    /**
307952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * Checks any blacklists (set in system settings) to see whether a certain
308952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * tag is allowed.  Entries with disabled tags will be dropped immediately,
309952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * so you can save the work of actually constructing and sending the data.
310952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     *
311952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @param tag that would be used in {@link #addText} or {@link #addFile}
312952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @return whether events with that tag would be accepted
313952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     */
314952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    public boolean isTagEnabled(String tag) {
315952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        try { return mService.isTagEnabled(tag); } catch (RemoteException e) { return false; }
316952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    }
317952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
318952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    /**
31930f5c8fede241e7560f4a33e48af3a4627fd5efcBrad Fitzpatrick     * Gets the next entry from the drop box <em>after</em> the specified time.
32030f5c8fede241e7560f4a33e48af3a4627fd5efcBrad Fitzpatrick     * Requires <code>android.permission.READ_LOGS</code>.  You must always call
321952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * {@link Entry#close()} on the return value!
322952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     *
323952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @param tag of entry to look for, null for all tags
324952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @param msec time of the last entry seen
325952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     * @return the next entry, or null if there are no more entries
326952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor     */
327952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    public Entry getNextEntry(String tag, long msec) {
328952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor        try { return mService.getNextEntry(tag, msec); } catch (RemoteException e) { return null; }
329952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    }
330952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor
331952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    // TODO: It may be useful to have some sort of notification mechanism
332952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    // when data is added to the dropbox, for demand-driven readers --
333952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor    // for now readers need to poll the dropbox to find new data.
334952402704a175ba27f6c89dff1ada634c5ce5626Dan Egnor}
335