DropBoxManager.java revision 952402704a175ba27f6c89dff1ada634c5ce5626
1/* 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 19import android.util.Log; 20 21import com.android.internal.os.IDropBoxService; 22 23import java.io.ByteArrayInputStream; 24import java.io.File; 25import java.io.FileInputStream; 26import java.io.IOException; 27import java.io.InputStream; 28import java.util.zip.GZIPInputStream; 29 30/** 31 * Enqueues chunks of data (from various sources -- application crashes, kernel 32 * log records, etc.). The queue is size bounded and will drop old data if the 33 * enqueued data exceeds the maximum size. You can think of this as a 34 * persistent, system-wide, blob-oriented "logcat". 35 * 36 * <p>You can obtain an instance of this class by calling 37 * {@link android.content.Context#getSystemService} 38 * with {@link android.content.Context#DROPBOX_SERVICE}. 39 * 40 * <p>DropBox entries are not sent anywhere directly, but other system services 41 * and debugging tools may scan and upload entries for processing. 42 * 43 * {@pending} 44 */ 45public class DropBox { 46 private static final String TAG = "DropBox"; 47 private final IDropBoxService mService; 48 49 /** Flag value: Entry's content was deleted to save space. */ 50 public static final int IS_EMPTY = 1; 51 52 /** Flag value: Content is human-readable UTF-8 text (can be combined with IS_GZIPPED). */ 53 public static final int IS_TEXT = 2; 54 55 /** Flag value: Content can be decompressed with {@link GZIPOutputStream}. */ 56 public static final int IS_GZIPPED = 4; 57 58 /** 59 * A single entry retrieved from the drop box. 60 * This may include a reference to a stream, so you must call 61 * {@link #close()} when you are done using it. 62 */ 63 public static class Entry implements Parcelable { 64 private final String mTag; 65 private final long mTimeMillis; 66 67 private final byte[] mData; 68 private final ParcelFileDescriptor mFileDescriptor; 69 private final int mFlags; 70 71 /** Create a new empty Entry with no contents. */ 72 public Entry(String tag, long millis) { 73 this(tag, millis, (Object) null, IS_EMPTY); 74 } 75 76 /** Create a new Entry with plain text contents. */ 77 public Entry(String tag, long millis, String text) { 78 this(tag, millis, (Object) text.getBytes(), IS_TEXT); 79 } 80 81 /** 82 * Create a new Entry with byte array contents. 83 * The data array must not be modified after creating this entry. 84 */ 85 public Entry(String tag, long millis, byte[] data, int flags) { 86 this(tag, millis, (Object) data, flags); 87 } 88 89 /** 90 * Create a new Entry with streaming data contents. 91 * Takes ownership of the ParcelFileDescriptor. 92 */ 93 public Entry(String tag, long millis, ParcelFileDescriptor data, int flags) { 94 this(tag, millis, (Object) data, flags); 95 } 96 97 /** 98 * Create a new Entry with the contents read from a file. 99 * The file will be read when the entry's contents are requested. 100 */ 101 public Entry(String tag, long millis, File data, int flags) throws IOException { 102 this(tag, millis, (Object) ParcelFileDescriptor.open( 103 data, ParcelFileDescriptor.MODE_READ_ONLY), flags); 104 } 105 106 /** Internal constructor for CREATOR.createFromParcel(). */ 107 private Entry(String tag, long millis, Object value, int flags) { 108 if (tag == null) throw new NullPointerException(); 109 if (((flags & IS_EMPTY) != 0) != (value == null)) throw new IllegalArgumentException(); 110 111 mTag = tag; 112 mTimeMillis = millis; 113 mFlags = flags; 114 115 if (value == null) { 116 mData = null; 117 mFileDescriptor = null; 118 } else if (value instanceof byte[]) { 119 mData = (byte[]) value; 120 mFileDescriptor = null; 121 } else if (value instanceof ParcelFileDescriptor) { 122 mData = null; 123 mFileDescriptor = (ParcelFileDescriptor) value; 124 } else { 125 throw new IllegalArgumentException(); 126 } 127 } 128 129 /** Close the input stream associated with this entry. */ 130 public void close() { 131 try { if (mFileDescriptor != null) mFileDescriptor.close(); } catch (IOException e) { } 132 } 133 134 /** @return the tag originally attached to the entry. */ 135 public String getTag() { return mTag; } 136 137 /** @return time when the entry was originally created. */ 138 public long getTimeMillis() { return mTimeMillis; } 139 140 /** @return flags describing the content returned by @{link #getInputStream()}. */ 141 public int getFlags() { return mFlags & ~IS_GZIPPED; } // getInputStream() decompresses. 142 143 /** 144 * @param maxBytes of string to return (will truncate at this length). 145 * @return the uncompressed text contents of the entry, null if the entry is not text. 146 */ 147 public String getText(int maxBytes) { 148 if ((mFlags & IS_TEXT) == 0) return null; 149 if (mData != null) return new String(mData, 0, Math.min(maxBytes, mData.length)); 150 151 InputStream is = null; 152 try { 153 is = getInputStream(); 154 byte[] buf = new byte[maxBytes]; 155 return new String(buf, 0, Math.max(0, is.read(buf))); 156 } catch (IOException e) { 157 return null; 158 } finally { 159 try { if (is != null) is.close(); } catch (IOException e) {} 160 } 161 } 162 163 /** @return the uncompressed contents of the entry, or null if the contents were lost */ 164 public InputStream getInputStream() throws IOException { 165 InputStream is; 166 if (mData != null) { 167 is = new ByteArrayInputStream(mData); 168 } else if (mFileDescriptor != null) { 169 is = new ParcelFileDescriptor.AutoCloseInputStream(mFileDescriptor); 170 } else { 171 return null; 172 } 173 return (mFlags & IS_GZIPPED) != 0 ? new GZIPInputStream(is) : is; 174 } 175 176 public static final Parcelable.Creator<Entry> CREATOR = new Parcelable.Creator() { 177 public Entry[] newArray(int size) { return new Entry[size]; } 178 public Entry createFromParcel(Parcel in) { 179 return new Entry( 180 in.readString(), in.readLong(), in.readValue(null), in.readInt()); 181 } 182 }; 183 184 public int describeContents() { 185 return mFileDescriptor != null ? Parcelable.CONTENTS_FILE_DESCRIPTOR : 0; 186 } 187 188 public void writeToParcel(Parcel out, int flags) { 189 out.writeString(mTag); 190 out.writeLong(mTimeMillis); 191 if (mFileDescriptor != null) { 192 out.writeValue(mFileDescriptor); 193 } else { 194 out.writeValue(mData); 195 } 196 out.writeInt(mFlags); 197 } 198 } 199 200 /** {@hide} */ 201 public DropBox(IDropBoxService service) { mService = service; } 202 203 /** 204 * Create a dummy instance for testing. All methods will fail unless 205 * overridden with an appropriate mock implementation. To obtain a 206 * functional instance, use {@link android.content.Context#getSystemService}. 207 */ 208 protected DropBox() { mService = null; } 209 210 /** 211 * Stores human-readable text. The data may be discarded eventually (or even 212 * immediately) if space is limited, or ignored entirely if the tag has been 213 * blocked (see {@link #isTagEnabled}). 214 * 215 * @param tag describing the type of entry being stored 216 * @param data value to store 217 */ 218 public void addText(String tag, String data) { 219 try { mService.add(new Entry(tag, 0, data)); } catch (RemoteException e) {} 220 } 221 222 /** 223 * Stores binary data, which may be ignored or discarded as with {@link #addText}. 224 * 225 * @param tag describing the type of entry being stored 226 * @param data value to store 227 * @param flags describing the data 228 */ 229 public void addData(String tag, byte[] data, int flags) { 230 if (data == null) throw new NullPointerException(); 231 try { mService.add(new Entry(tag, 0, data, flags)); } catch (RemoteException e) {} 232 } 233 234 /** 235 * Stores data read from a file descriptor. The data may be ignored or 236 * discarded as with {@link #addText}. You must close your 237 * ParcelFileDescriptor object after calling this method! 238 * 239 * @param tag describing the type of entry being stored 240 * @param fd file descriptor to read from 241 * @param flags describing the data 242 */ 243 public void addFile(String tag, ParcelFileDescriptor fd, int flags) { 244 if (fd == null) throw new NullPointerException(); 245 try { mService.add(new Entry(tag, 0, fd, flags)); } catch (RemoteException e) {} 246 } 247 248 /** 249 * Checks any blacklists (set in system settings) to see whether a certain 250 * tag is allowed. Entries with disabled tags will be dropped immediately, 251 * so you can save the work of actually constructing and sending the data. 252 * 253 * @param tag that would be used in {@link #addText} or {@link #addFile} 254 * @return whether events with that tag would be accepted 255 */ 256 public boolean isTagEnabled(String tag) { 257 try { return mService.isTagEnabled(tag); } catch (RemoteException e) { return false; } 258 } 259 260 /** 261 * Gets the next entry from the drop box *after* the specified time. 262 * Requires android.permission.READ_LOGS. You must always call 263 * {@link Entry#close()} on the return value! 264 * 265 * @param tag of entry to look for, null for all tags 266 * @param msec time of the last entry seen 267 * @return the next entry, or null if there are no more entries 268 */ 269 public Entry getNextEntry(String tag, long msec) { 270 try { return mService.getNextEntry(tag, msec); } catch (RemoteException e) { return null; } 271 } 272 273 // TODO: It may be useful to have some sort of notification mechanism 274 // when data is added to the dropbox, for demand-driven readers -- 275 // for now readers need to poll the dropbox to find new data. 276} 277