Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 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 | |
| 17 | package android.os; |
| 18 | |
Jeff Sharkey | c36c3b9 | 2018-04-19 15:22:45 -0600 | [diff] [blame] | 19 | import static android.Manifest.permission.PACKAGE_USAGE_STATS; |
| 20 | import static android.Manifest.permission.READ_LOGS; |
| 21 | |
| 22 | import android.annotation.Nullable; |
| 23 | import android.annotation.RequiresPermission; |
Jeff Sharkey | 0f3f60b | 2017-04-24 18:06:20 -0600 | [diff] [blame] | 24 | import android.annotation.SdkConstant; |
| 25 | import android.annotation.SdkConstant.SdkConstantType; |
Jeff Sharkey | c36c3b9 | 2018-04-19 15:22:45 -0600 | [diff] [blame] | 26 | import android.annotation.SystemService; |
Jeff Sharkey | b8e8a91 | 2016-03-09 16:27:40 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.util.Log; |
| 29 | |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 30 | import com.android.internal.os.IDropBoxManagerService; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 31 | |
| 32 | import java.io.ByteArrayInputStream; |
Brad Fitzpatrick | cc792c4 | 2010-10-08 09:50:30 -0700 | [diff] [blame] | 33 | import java.io.Closeable; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 34 | import java.io.File; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.util.zip.GZIPInputStream; |
| 38 | |
| 39 | /** |
| 40 | * Enqueues chunks of data (from various sources -- application crashes, kernel |
| 41 | * log records, etc.). The queue is size bounded and will drop old data if the |
| 42 | * enqueued data exceeds the maximum size. You can think of this as a |
| 43 | * persistent, system-wide, blob-oriented "logcat". |
| 44 | * |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 45 | * <p>DropBoxManager entries are not sent anywhere directly, but other system |
| 46 | * services and debugging tools may scan and upload entries for processing. |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 47 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 48 | @SystemService(Context.DROPBOX_SERVICE) |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 49 | public class DropBoxManager { |
| 50 | private static final String TAG = "DropBoxManager"; |
Jeff Sharkey | b8e8a91 | 2016-03-09 16:27:40 -0700 | [diff] [blame] | 51 | |
| 52 | private final Context mContext; |
Dan Egnor | f18a01c | 2009-11-12 11:32:50 -0800 | [diff] [blame] | 53 | private final IDropBoxManagerService mService; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 54 | |
| 55 | /** Flag value: Entry's content was deleted to save space. */ |
| 56 | public static final int IS_EMPTY = 1; |
| 57 | |
| 58 | /** Flag value: Content is human-readable UTF-8 text (can be combined with IS_GZIPPED). */ |
| 59 | public static final int IS_TEXT = 2; |
| 60 | |
Dan Egnor | e3cfe2d | 2009-11-14 16:20:04 -0800 | [diff] [blame] | 61 | /** Flag value: Content can be decompressed with {@link java.util.zip.GZIPOutputStream}. */ |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 62 | public static final int IS_GZIPPED = 4; |
| 63 | |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 64 | /** Flag value for serialization only: Value is a byte array, not a file descriptor */ |
| 65 | private static final int HAS_BYTE_ARRAY = 8; |
| 66 | |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 67 | /** |
Hakan Still | b247536 | 2010-12-07 14:05:55 +0100 | [diff] [blame] | 68 | * Broadcast Action: This is broadcast when a new entry is added in the dropbox. |
| 69 | * You must hold the {@link android.Manifest.permission#READ_LOGS} permission |
| 70 | * in order to receive this broadcast. |
| 71 | * |
| 72 | * <p class="note">This is a protected intent that can only be sent |
| 73 | * by the system. |
| 74 | */ |
Jeff Sharkey | 0f3f60b | 2017-04-24 18:06:20 -0600 | [diff] [blame] | 75 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Hakan Still | b247536 | 2010-12-07 14:05:55 +0100 | [diff] [blame] | 76 | public static final String ACTION_DROPBOX_ENTRY_ADDED = |
| 77 | "android.intent.action.DROPBOX_ENTRY_ADDED"; |
| 78 | |
| 79 | /** |
| 80 | * Extra for {@link android.os.DropBoxManager#ACTION_DROPBOX_ENTRY_ADDED}: |
| 81 | * string containing the dropbox tag. |
| 82 | */ |
| 83 | public static final String EXTRA_TAG = "tag"; |
| 84 | |
| 85 | /** |
| 86 | * Extra for {@link android.os.DropBoxManager#ACTION_DROPBOX_ENTRY_ADDED}: |
| 87 | * long integer value containing time (in milliseconds since January 1, 1970 00:00:00 UTC) |
| 88 | * when the entry was created. |
| 89 | */ |
| 90 | public static final String EXTRA_TIME = "time"; |
| 91 | |
| 92 | /** |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 93 | * A single entry retrieved from the drop box. |
| 94 | * This may include a reference to a stream, so you must call |
| 95 | * {@link #close()} when you are done using it. |
| 96 | */ |
Brad Fitzpatrick | cc792c4 | 2010-10-08 09:50:30 -0700 | [diff] [blame] | 97 | public static class Entry implements Parcelable, Closeable { |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 98 | private final String mTag; |
| 99 | private final long mTimeMillis; |
| 100 | |
| 101 | private final byte[] mData; |
| 102 | private final ParcelFileDescriptor mFileDescriptor; |
| 103 | private final int mFlags; |
| 104 | |
| 105 | /** Create a new empty Entry with no contents. */ |
| 106 | public Entry(String tag, long millis) { |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 107 | if (tag == null) throw new NullPointerException("tag == null"); |
| 108 | |
| 109 | mTag = tag; |
| 110 | mTimeMillis = millis; |
| 111 | mData = null; |
| 112 | mFileDescriptor = null; |
| 113 | mFlags = IS_EMPTY; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** Create a new Entry with plain text contents. */ |
| 117 | public Entry(String tag, long millis, String text) { |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 118 | if (tag == null) throw new NullPointerException("tag == null"); |
| 119 | if (text == null) throw new NullPointerException("text == null"); |
| 120 | |
| 121 | mTag = tag; |
| 122 | mTimeMillis = millis; |
| 123 | mData = text.getBytes(); |
| 124 | mFileDescriptor = null; |
| 125 | mFlags = IS_TEXT; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Create a new Entry with byte array contents. |
| 130 | * The data array must not be modified after creating this entry. |
| 131 | */ |
| 132 | public Entry(String tag, long millis, byte[] data, int flags) { |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 133 | if (tag == null) throw new NullPointerException("tag == null"); |
| 134 | if (((flags & IS_EMPTY) != 0) != (data == null)) { |
| 135 | throw new IllegalArgumentException("Bad flags: " + flags); |
| 136 | } |
| 137 | |
| 138 | mTag = tag; |
| 139 | mTimeMillis = millis; |
| 140 | mData = data; |
| 141 | mFileDescriptor = null; |
| 142 | mFlags = flags; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Create a new Entry with streaming data contents. |
| 147 | * Takes ownership of the ParcelFileDescriptor. |
| 148 | */ |
| 149 | public Entry(String tag, long millis, ParcelFileDescriptor data, int flags) { |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 150 | if (tag == null) throw new NullPointerException("tag == null"); |
| 151 | if (((flags & IS_EMPTY) != 0) != (data == null)) { |
| 152 | throw new IllegalArgumentException("Bad flags: " + flags); |
| 153 | } |
| 154 | |
| 155 | mTag = tag; |
| 156 | mTimeMillis = millis; |
| 157 | mData = null; |
| 158 | mFileDescriptor = data; |
| 159 | mFlags = flags; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Create a new Entry with the contents read from a file. |
| 164 | * The file will be read when the entry's contents are requested. |
| 165 | */ |
| 166 | public Entry(String tag, long millis, File data, int flags) throws IOException { |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 167 | if (tag == null) throw new NullPointerException("tag == null"); |
| 168 | if ((flags & IS_EMPTY) != 0) throw new IllegalArgumentException("Bad flags: " + flags); |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 169 | |
| 170 | mTag = tag; |
| 171 | mTimeMillis = millis; |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 172 | mData = null; |
| 173 | mFileDescriptor = ParcelFileDescriptor.open(data, ParcelFileDescriptor.MODE_READ_ONLY); |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 174 | mFlags = flags; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** Close the input stream associated with this entry. */ |
| 178 | public void close() { |
| 179 | try { if (mFileDescriptor != null) mFileDescriptor.close(); } catch (IOException e) { } |
| 180 | } |
| 181 | |
| 182 | /** @return the tag originally attached to the entry. */ |
| 183 | public String getTag() { return mTag; } |
| 184 | |
| 185 | /** @return time when the entry was originally created. */ |
| 186 | public long getTimeMillis() { return mTimeMillis; } |
| 187 | |
Brad Fitzpatrick | 95173b1 | 2010-10-07 10:07:34 -0700 | [diff] [blame] | 188 | /** @return flags describing the content returned by {@link #getInputStream()}. */ |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 189 | public int getFlags() { return mFlags & ~IS_GZIPPED; } // getInputStream() decompresses. |
| 190 | |
| 191 | /** |
| 192 | * @param maxBytes of string to return (will truncate at this length). |
| 193 | * @return the uncompressed text contents of the entry, null if the entry is not text. |
| 194 | */ |
| 195 | public String getText(int maxBytes) { |
| 196 | if ((mFlags & IS_TEXT) == 0) return null; |
| 197 | if (mData != null) return new String(mData, 0, Math.min(maxBytes, mData.length)); |
| 198 | |
| 199 | InputStream is = null; |
| 200 | try { |
| 201 | is = getInputStream(); |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 202 | if (is == null) return null; |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 203 | byte[] buf = new byte[maxBytes]; |
Christian Lindeberg | e9f1881 | 2010-11-16 09:57:54 +0100 | [diff] [blame] | 204 | int readBytes = 0; |
| 205 | int n = 0; |
| 206 | while (n >= 0 && (readBytes += n) < maxBytes) { |
| 207 | n = is.read(buf, readBytes, maxBytes - readBytes); |
| 208 | } |
| 209 | return new String(buf, 0, readBytes); |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 210 | } catch (IOException e) { |
| 211 | return null; |
| 212 | } finally { |
| 213 | try { if (is != null) is.close(); } catch (IOException e) {} |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /** @return the uncompressed contents of the entry, or null if the contents were lost */ |
| 218 | public InputStream getInputStream() throws IOException { |
| 219 | InputStream is; |
| 220 | if (mData != null) { |
| 221 | is = new ByteArrayInputStream(mData); |
| 222 | } else if (mFileDescriptor != null) { |
| 223 | is = new ParcelFileDescriptor.AutoCloseInputStream(mFileDescriptor); |
| 224 | } else { |
| 225 | return null; |
| 226 | } |
| 227 | return (mFlags & IS_GZIPPED) != 0 ? new GZIPInputStream(is) : is; |
| 228 | } |
| 229 | |
| 230 | public static final Parcelable.Creator<Entry> CREATOR = new Parcelable.Creator() { |
| 231 | public Entry[] newArray(int size) { return new Entry[size]; } |
| 232 | public Entry createFromParcel(Parcel in) { |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 233 | String tag = in.readString(); |
| 234 | long millis = in.readLong(); |
| 235 | int flags = in.readInt(); |
| 236 | if ((flags & HAS_BYTE_ARRAY) != 0) { |
| 237 | return new Entry(tag, millis, in.createByteArray(), flags & ~HAS_BYTE_ARRAY); |
| 238 | } else { |
Tim Kilbourn | ba95323 | 2015-06-02 15:57:45 -0700 | [diff] [blame] | 239 | ParcelFileDescriptor pfd = ParcelFileDescriptor.CREATOR.createFromParcel(in); |
| 240 | return new Entry(tag, millis, pfd, flags); |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 241 | } |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 242 | } |
| 243 | }; |
| 244 | |
| 245 | public int describeContents() { |
| 246 | return mFileDescriptor != null ? Parcelable.CONTENTS_FILE_DESCRIPTOR : 0; |
| 247 | } |
| 248 | |
| 249 | public void writeToParcel(Parcel out, int flags) { |
| 250 | out.writeString(mTag); |
| 251 | out.writeLong(mTimeMillis); |
| 252 | if (mFileDescriptor != null) { |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 253 | out.writeInt(mFlags & ~HAS_BYTE_ARRAY); // Clear bit just to be safe |
| 254 | mFileDescriptor.writeToParcel(out, flags); |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 255 | } else { |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 256 | out.writeInt(mFlags | HAS_BYTE_ARRAY); |
| 257 | out.writeByteArray(mData); |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 258 | } |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | /** {@hide} */ |
Jeff Sharkey | b8e8a91 | 2016-03-09 16:27:40 -0700 | [diff] [blame] | 263 | public DropBoxManager(Context context, IDropBoxManagerService service) { |
| 264 | mContext = context; |
| 265 | mService = service; |
| 266 | } |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 267 | |
| 268 | /** |
| 269 | * Create a dummy instance for testing. All methods will fail unless |
| 270 | * overridden with an appropriate mock implementation. To obtain a |
| 271 | * functional instance, use {@link android.content.Context#getSystemService}. |
| 272 | */ |
Jeff Sharkey | b8e8a91 | 2016-03-09 16:27:40 -0700 | [diff] [blame] | 273 | protected DropBoxManager() { |
| 274 | mContext = null; |
| 275 | mService = null; |
| 276 | } |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 277 | |
| 278 | /** |
| 279 | * Stores human-readable text. The data may be discarded eventually (or even |
| 280 | * immediately) if space is limited, or ignored entirely if the tag has been |
| 281 | * blocked (see {@link #isTagEnabled}). |
| 282 | * |
| 283 | * @param tag describing the type of entry being stored |
| 284 | * @param data value to store |
| 285 | */ |
| 286 | public void addText(String tag, String data) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 287 | try { |
| 288 | mService.add(new Entry(tag, 0, data)); |
| 289 | } catch (RemoteException e) { |
Jeff Sharkey | b8e8a91 | 2016-03-09 16:27:40 -0700 | [diff] [blame] | 290 | if (e instanceof TransactionTooLargeException |
| 291 | && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) { |
| 292 | Log.e(TAG, "App sent too much data, so it was ignored", e); |
| 293 | return; |
| 294 | } |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 295 | throw e.rethrowFromSystemServer(); |
| 296 | } |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Stores binary data, which may be ignored or discarded as with {@link #addText}. |
| 301 | * |
| 302 | * @param tag describing the type of entry being stored |
| 303 | * @param data value to store |
| 304 | * @param flags describing the data |
| 305 | */ |
| 306 | public void addData(String tag, byte[] data, int flags) { |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 307 | if (data == null) throw new NullPointerException("data == null"); |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 308 | try { |
| 309 | mService.add(new Entry(tag, 0, data, flags)); |
| 310 | } catch (RemoteException e) { |
Jeff Sharkey | b8e8a91 | 2016-03-09 16:27:40 -0700 | [diff] [blame] | 311 | if (e instanceof TransactionTooLargeException |
| 312 | && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) { |
| 313 | Log.e(TAG, "App sent too much data, so it was ignored", e); |
| 314 | return; |
| 315 | } |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 316 | throw e.rethrowFromSystemServer(); |
| 317 | } |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /** |
Dan Egnor | eb7a7d5 | 2009-11-25 12:38:00 -0800 | [diff] [blame] | 321 | * Stores the contents of a file, which may be ignored or discarded as with |
| 322 | * {@link #addText}. |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 323 | * |
| 324 | * @param tag describing the type of entry being stored |
Dan Egnor | eb7a7d5 | 2009-11-25 12:38:00 -0800 | [diff] [blame] | 325 | * @param file to read from |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 326 | * @param flags describing the data |
Dan Egnor | eb7a7d5 | 2009-11-25 12:38:00 -0800 | [diff] [blame] | 327 | * @throws IOException if the file can't be opened |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 328 | */ |
Dan Egnor | eb7a7d5 | 2009-11-25 12:38:00 -0800 | [diff] [blame] | 329 | public void addFile(String tag, File file, int flags) throws IOException { |
Dan Egnor | 6e6d60d | 2010-07-20 15:24:09 -0700 | [diff] [blame] | 330 | if (file == null) throw new NullPointerException("file == null"); |
Dan Egnor | eb7a7d5 | 2009-11-25 12:38:00 -0800 | [diff] [blame] | 331 | Entry entry = new Entry(tag, 0, file, flags); |
| 332 | try { |
Brad Fitzpatrick | 1441894 | 2010-06-14 08:58:26 -0700 | [diff] [blame] | 333 | mService.add(entry); |
Dan Egnor | eb7a7d5 | 2009-11-25 12:38:00 -0800 | [diff] [blame] | 334 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 335 | throw e.rethrowFromSystemServer(); |
Dan Egnor | eb7a7d5 | 2009-11-25 12:38:00 -0800 | [diff] [blame] | 336 | } finally { |
| 337 | entry.close(); |
| 338 | } |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Checks any blacklists (set in system settings) to see whether a certain |
| 343 | * tag is allowed. Entries with disabled tags will be dropped immediately, |
| 344 | * so you can save the work of actually constructing and sending the data. |
| 345 | * |
| 346 | * @param tag that would be used in {@link #addText} or {@link #addFile} |
| 347 | * @return whether events with that tag would be accepted |
| 348 | */ |
| 349 | public boolean isTagEnabled(String tag) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 350 | try { |
| 351 | return mService.isTagEnabled(tag); |
| 352 | } catch (RemoteException e) { |
| 353 | throw e.rethrowFromSystemServer(); |
| 354 | } |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
Brad Fitzpatrick | 30f5c8f | 2010-10-07 10:48:20 -0700 | [diff] [blame] | 358 | * Gets the next entry from the drop box <em>after</em> the specified time. |
Jeff Sharkey | c36c3b9 | 2018-04-19 15:22:45 -0600 | [diff] [blame] | 359 | * You must always call {@link Entry#close()} on the return value! |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 360 | * |
| 361 | * @param tag of entry to look for, null for all tags |
| 362 | * @param msec time of the last entry seen |
| 363 | * @return the next entry, or null if there are no more entries |
| 364 | */ |
Jeff Sharkey | c36c3b9 | 2018-04-19 15:22:45 -0600 | [diff] [blame] | 365 | @RequiresPermission(allOf = { READ_LOGS, PACKAGE_USAGE_STATS }) |
| 366 | public @Nullable Entry getNextEntry(String tag, long msec) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 367 | try { |
Jeff Sharkey | c36c3b9 | 2018-04-19 15:22:45 -0600 | [diff] [blame] | 368 | return mService.getNextEntry(tag, msec, mContext.getOpPackageName()); |
| 369 | } catch (SecurityException e) { |
| 370 | if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) { |
| 371 | throw e; |
| 372 | } else { |
| 373 | Log.w(TAG, e.getMessage()); |
| 374 | return null; |
| 375 | } |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 376 | } catch (RemoteException e) { |
| 377 | throw e.rethrowFromSystemServer(); |
| 378 | } |
Dan Egnor | 9524027 | 2009-10-27 18:23:39 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | // TODO: It may be useful to have some sort of notification mechanism |
| 382 | // when data is added to the dropbox, for demand-driven readers -- |
| 383 | // for now readers need to poll the dropbox to find new data. |
| 384 | } |