| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import static android.Manifest.permission.PACKAGE_USAGE_STATS; |
| 20 |
import static android.Manifest.permission.READ_LOGS; |
| 21 |
|
| 22 |
import android.annotation.BytesLong; |
| 23 |
import android.annotation.CurrentTimeMillisLong; |
| 24 |
import android.annotation.IntDef; |
| 25 |
import android.annotation.NonNull; |
| 26 |
import android.annotation.Nullable; |
| 27 |
import android.annotation.RequiresPermission; |
| 28 |
import android.annotation.SdkConstant; |
| 29 |
import android.annotation.SdkConstant.SdkConstantType; |
| 30 |
import android.annotation.SystemService; |
| 31 |
import android.compat.annotation.UnsupportedAppUsage; |
| 32 |
import android.content.Context; |
| 33 |
import android.util.Log; |
| 34 |
|
| 35 |
import com.android.internal.os.IDropBoxManagerService; |
| 36 |
|
| 37 |
import java.io.BufferedInputStream; |
| 38 |
import java.io.ByteArrayInputStream; |
| 39 |
import java.io.Closeable; |
| 40 |
import java.io.File; |
| 41 |
import java.io.IOException; |
| 42 |
import java.io.InputStream; |
| 43 |
import java.lang.annotation.Retention; |
| 44 |
import java.lang.annotation.RetentionPolicy; |
| 45 |
import java.nio.charset.StandardCharsets; |
| 46 |
import java.util.zip.GZIPInputStream; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
@SystemService(Context.DROPBOX_SERVICE) |
| 58 |
public class DropBoxManager { |
| 59 |
private static final String TAG = "DropBoxManager"; |
| 60 |
|
| 61 |
private final Context mContext; |
| 62 |
@UnsupportedAppUsage |
| 63 |
private final IDropBoxManagerService mService; |
| 64 |
|
| 65 |
|
| 66 |
@IntDef(flag = true, prefix = { "IS_" }, value = { IS_EMPTY, IS_TEXT, IS_GZIPPED }) |
| 67 |
@Retention(RetentionPolicy.SOURCE) |
| 68 |
public @interface Flags {} |
| 69 |
|
| 70 |
|
| 71 |
public static final int IS_EMPTY = 1; |
| 72 |
|
| 73 |
|
| 74 |
public static final int IS_TEXT = 2; |
| 75 |
|
| 76 |
|
| 77 |
public static final int IS_GZIPPED = 4; |
| 78 |
|
| 79 |
|
| 80 |
private static final int HAS_BYTE_ARRAY = 8; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 92 |
public static final String ACTION_DROPBOX_ENTRY_ADDED = |
| 93 |
"android.intent.action.DROPBOX_ENTRY_ADDED"; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static final String EXTRA_TAG = "tag"; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public static final String EXTRA_TIME = "time"; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public static final String EXTRA_DROPPED_COUNT = "android.os.extra.DROPPED_COUNT"; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public static class Entry implements Parcelable, Closeable { |
| 121 |
private final @NonNull String mTag; |
| 122 |
private final @CurrentTimeMillisLong long mTimeMillis; |
| 123 |
|
| 124 |
private final @Nullable byte[] mData; |
| 125 |
private final @Nullable ParcelFileDescriptor mFileDescriptor; |
| 126 |
private final @Flags int mFlags; |
| 127 |
|
| 128 |
|
| 129 |
public Entry(@NonNull String tag, @CurrentTimeMillisLong long millis) { |
| 130 |
if (tag == null) throw new NullPointerException("tag == null"); |
| 131 |
|
| 132 |
mTag = tag; |
| 133 |
mTimeMillis = millis; |
| 134 |
mData = null; |
| 135 |
mFileDescriptor = null; |
| 136 |
mFlags = IS_EMPTY; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
public Entry(@NonNull String tag, @CurrentTimeMillisLong long millis, |
| 141 |
@NonNull String text) { |
| 142 |
if (tag == null) throw new NullPointerException("tag == null"); |
| 143 |
if (text == null) throw new NullPointerException("text == null"); |
| 144 |
|
| 145 |
mTag = tag; |
| 146 |
mTimeMillis = millis; |
| 147 |
mData = text.getBytes(StandardCharsets.UTF_8); |
| 148 |
mFileDescriptor = null; |
| 149 |
mFlags = IS_TEXT; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public Entry(@NonNull String tag, @CurrentTimeMillisLong long millis, |
| 157 |
@Nullable byte[] data, @Flags int flags) { |
| 158 |
if (tag == null) throw new NullPointerException("tag == null"); |
| 159 |
if (((flags & IS_EMPTY) != 0) != (data == null)) { |
| 160 |
throw new IllegalArgumentException("Bad flags: " + flags); |
| 161 |
} |
| 162 |
|
| 163 |
mTag = tag; |
| 164 |
mTimeMillis = millis; |
| 165 |
mData = data; |
| 166 |
mFileDescriptor = null; |
| 167 |
mFlags = flags; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public Entry(@NonNull String tag, @CurrentTimeMillisLong long millis, |
| 175 |
@Nullable ParcelFileDescriptor data, @Flags int flags) { |
| 176 |
if (tag == null) throw new NullPointerException("tag == null"); |
| 177 |
if (((flags & IS_EMPTY) != 0) != (data == null)) { |
| 178 |
throw new IllegalArgumentException("Bad flags: " + flags); |
| 179 |
} |
| 180 |
|
| 181 |
mTag = tag; |
| 182 |
mTimeMillis = millis; |
| 183 |
mData = null; |
| 184 |
mFileDescriptor = data; |
| 185 |
mFlags = flags; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public Entry(@NonNull String tag, @CurrentTimeMillisLong long millis, |
| 193 |
@NonNull File data, @Flags int flags) throws IOException { |
| 194 |
if (tag == null) throw new NullPointerException("tag == null"); |
| 195 |
if ((flags & IS_EMPTY) != 0) throw new IllegalArgumentException("Bad flags: " + flags); |
| 196 |
|
| 197 |
mTag = tag; |
| 198 |
mTimeMillis = millis; |
| 199 |
mData = null; |
| 200 |
mFileDescriptor = ParcelFileDescriptor.open(data, ParcelFileDescriptor.MODE_READ_ONLY); |
| 201 |
mFlags = flags; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
public void close() { |
| 206 |
try { if (mFileDescriptor != null) mFileDescriptor.close(); } catch (IOException e) { } |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
public @NonNull String getTag() { |
| 211 |
return mTag; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
public @CurrentTimeMillisLong long getTimeMillis() { |
| 216 |
return mTimeMillis; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
public @Flags int getFlags() { |
| 221 |
|
| 222 |
return mFlags & ~IS_GZIPPED; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public @Nullable String getText(@BytesLong int maxBytes) { |
| 230 |
if ((mFlags & IS_TEXT) == 0) return null; |
| 231 |
if (mData != null) return new String(mData, 0, Math.min(maxBytes, mData.length)); |
| 232 |
|
| 233 |
InputStream is = null; |
| 234 |
try { |
| 235 |
is = getInputStream(); |
| 236 |
if (is == null) return null; |
| 237 |
byte[] buf = new byte[maxBytes]; |
| 238 |
int readBytes = 0; |
| 239 |
int n = 0; |
| 240 |
while (n >= 0 && (readBytes += n) < maxBytes) { |
| 241 |
n = is.read(buf, readBytes, maxBytes - readBytes); |
| 242 |
} |
| 243 |
return new String(buf, 0, readBytes); |
| 244 |
} catch (IOException e) { |
| 245 |
return null; |
| 246 |
} finally { |
| 247 |
try { if (is != null) is.close(); } catch (IOException e) {} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
public @Nullable InputStream getInputStream() throws IOException { |
| 253 |
InputStream is; |
| 254 |
if (mData != null) { |
| 255 |
is = new ByteArrayInputStream(mData); |
| 256 |
} else if (mFileDescriptor != null) { |
| 257 |
is = new ParcelFileDescriptor.AutoCloseInputStream(mFileDescriptor); |
| 258 |
} else { |
| 259 |
return null; |
| 260 |
} |
| 261 |
return (mFlags & IS_GZIPPED) != 0 |
| 262 |
? new GZIPInputStream(new BufferedInputStream(is)) : is; |
| 263 |
} |
| 264 |
|
| 265 |
public static final @android.annotation.NonNull Parcelable.Creator<Entry> CREATOR = new Parcelable.Creator() { |
| 266 |
public Entry[] newArray(int size) { return new Entry[size]; } |
| 267 |
public Entry createFromParcel(Parcel in) { |
| 268 |
String tag = in.readString(); |
| 269 |
long millis = in.readLong(); |
| 270 |
int flags = in.readInt(); |
| 271 |
if ((flags & HAS_BYTE_ARRAY) != 0) { |
| 272 |
return new Entry(tag, millis, in.createByteArray(), flags & ~HAS_BYTE_ARRAY); |
| 273 |
} else { |
| 274 |
ParcelFileDescriptor pfd = ParcelFileDescriptor.CREATOR.createFromParcel(in); |
| 275 |
return new Entry(tag, millis, pfd, flags); |
| 276 |
} |
| 277 |
} |
| 278 |
}; |
| 279 |
|
| 280 |
public int describeContents() { |
| 281 |
return mFileDescriptor != null ? Parcelable.CONTENTS_FILE_DESCRIPTOR : 0; |
| 282 |
} |
| 283 |
|
| 284 |
public void writeToParcel(Parcel out, int flags) { |
| 285 |
out.writeString(mTag); |
| 286 |
out.writeLong(mTimeMillis); |
| 287 |
if (mFileDescriptor != null) { |
| 288 |
out.writeInt(mFlags & ~HAS_BYTE_ARRAY); |
| 289 |
mFileDescriptor.writeToParcel(out, flags); |
| 290 |
} else { |
| 291 |
out.writeInt(mFlags | HAS_BYTE_ARRAY); |
| 292 |
out.writeByteArray(mData); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
public DropBoxManager(Context context, IDropBoxManagerService service) { |
| 299 |
mContext = context; |
| 300 |
mService = service; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
protected DropBoxManager() { |
| 309 |
mContext = null; |
| 310 |
mService = null; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public void addText(@NonNull String tag, @NonNull String data) { |
| 322 |
addData(tag, data.getBytes(StandardCharsets.UTF_8), IS_TEXT); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public void addData(@NonNull String tag, @Nullable byte[] data, @Flags int flags) { |
| 333 |
if (data == null) throw new NullPointerException("data == null"); |
| 334 |
try { |
| 335 |
mService.addData(tag, data, flags); |
| 336 |
} catch (RemoteException e) { |
| 337 |
if (e instanceof TransactionTooLargeException |
| 338 |
&& mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) { |
| 339 |
Log.e(TAG, "App sent too much data, so it was ignored", e); |
| 340 |
return; |
| 341 |
} |
| 342 |
throw e.rethrowFromSystemServer(); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public void addFile(@NonNull String tag, @NonNull File file, @Flags int flags) |
| 356 |
throws IOException { |
| 357 |
if (file == null) throw new NullPointerException("file == null"); |
| 358 |
try (ParcelFileDescriptor pfd = ParcelFileDescriptor.open(file, |
| 359 |
ParcelFileDescriptor.MODE_READ_ONLY)) { |
| 360 |
mService.addFile(tag, pfd, flags); |
| 361 |
} catch (RemoteException e) { |
| 362 |
throw e.rethrowFromSystemServer(); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
public boolean isTagEnabled(String tag) { |
| 375 |
try { |
| 376 |
return mService.isTagEnabled(tag); |
| 377 |
} catch (RemoteException e) { |
| 378 |
throw e.rethrowFromSystemServer(); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
@RequiresPermission(allOf = { READ_LOGS, PACKAGE_USAGE_STATS }) |
| 391 |
public @Nullable Entry getNextEntry(String tag, long msec) { |
| 392 |
try { |
| 393 |
return mService.getNextEntryWithAttribution(tag, msec, mContext.getOpPackageName(), |
| 394 |
mContext.getAttributionTag()); |
| 395 |
} catch (SecurityException e) { |
| 396 |
if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.P) { |
| 397 |
throw e; |
| 398 |
} else { |
| 399 |
Log.w(TAG, e.getMessage()); |
| 400 |
return null; |
| 401 |
} |
| 402 |
} catch (RemoteException e) { |
| 403 |
throw e.rethrowFromSystemServer(); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
} |