| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.RequiresPermission; |
| 22 |
import android.annotation.SdkConstant; |
| 23 |
import android.annotation.SdkConstant.SdkConstantType; |
| 24 |
import android.annotation.SystemApi; |
| 25 |
import android.annotation.SystemService; |
| 26 |
import android.annotation.TestApi; |
| 27 |
import android.compat.annotation.UnsupportedAppUsage; |
| 28 |
import android.content.ClipDescription; |
| 29 |
import android.content.ContentProviderClient; |
| 30 |
import android.content.ContentResolver; |
| 31 |
import android.content.ContentUris; |
| 32 |
import android.content.ContentValues; |
| 33 |
import android.content.Context; |
| 34 |
import android.database.Cursor; |
| 35 |
import android.database.CursorWrapper; |
| 36 |
import android.database.DatabaseUtils; |
| 37 |
import android.net.ConnectivityManager; |
| 38 |
import android.net.NetworkPolicyManager; |
| 39 |
import android.net.Uri; |
| 40 |
import android.os.Build; |
| 41 |
import android.os.Bundle; |
| 42 |
import android.os.Environment; |
| 43 |
import android.os.FileUtils; |
| 44 |
import android.os.ParcelFileDescriptor; |
| 45 |
import android.os.RemoteException; |
| 46 |
import android.provider.BaseColumns; |
| 47 |
import android.provider.Downloads; |
| 48 |
import android.provider.MediaStore; |
| 49 |
import android.provider.Settings; |
| 50 |
import android.provider.Settings.SettingNotFoundException; |
| 51 |
import android.text.TextUtils; |
| 52 |
import android.util.LongSparseArray; |
| 53 |
import android.util.Pair; |
| 54 |
import android.webkit.MimeTypeMap; |
| 55 |
|
| 56 |
import java.io.File; |
| 57 |
import java.io.FileNotFoundException; |
| 58 |
import java.util.ArrayList; |
| 59 |
import java.util.List; |
| 60 |
import java.util.Locale; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
@SystemService(Context.DOWNLOAD_SERVICE) |
| 76 |
public class DownloadManager { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public final static String COLUMN_ID = Downloads.Impl._ID; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public final static String COLUMN_TITLE = Downloads.Impl.COLUMN_TITLE; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public final static String COLUMN_DESCRIPTION = Downloads.Impl.COLUMN_DESCRIPTION; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public final static String COLUMN_URI = Downloads.Impl.COLUMN_URI; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public final static String COLUMN_MEDIA_TYPE = "media_type"; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public final static String COLUMN_TOTAL_SIZE_BYTES = "total_size"; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public final static String COLUMN_LOCAL_URI = "local_uri"; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
@Deprecated |
| 135 |
public final static String COLUMN_LOCAL_FILENAME = "local_filename"; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public final static String COLUMN_STATUS = Downloads.Impl.COLUMN_STATUS; |
| 141 |
|
| 142 |
|
| 143 |
public final static String COLUMN_FILE_NAME_HINT = Downloads.Impl.COLUMN_FILE_NAME_HINT; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public final static String COLUMN_REASON = "reason"; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public final static String COLUMN_BYTES_DOWNLOADED_SO_FAR = "bytes_so_far"; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public final static String COLUMN_LAST_MODIFIED_TIMESTAMP = "last_modified_timestamp"; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public static final String COLUMN_MEDIAPROVIDER_URI = Downloads.Impl.COLUMN_MEDIAPROVIDER_URI; |
| 181 |
|
| 182 |
|
| 183 |
public static final String COLUMN_DESTINATION = Downloads.Impl.COLUMN_DESTINATION; |
| 184 |
|
| 185 |
|
| 186 |
@TestApi |
| 187 |
public static final String COLUMN_MEDIASTORE_URI = Downloads.Impl.COLUMN_MEDIASTORE_URI; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public final static String COLUMN_ALLOW_WRITE = Downloads.Impl.COLUMN_ALLOW_WRITE; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public final static int STATUS_PENDING = 1 << 0; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public final static int STATUS_RUNNING = 1 << 1; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public final static int STATUS_PAUSED = 1 << 2; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public final static int STATUS_SUCCESSFUL = 1 << 3; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public final static int STATUS_FAILED = 1 << 4; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public final static int ERROR_UNKNOWN = 1000; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public final static int ERROR_FILE_ERROR = 1001; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public final static int ERROR_UNHANDLED_HTTP_CODE = 1002; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public final static int ERROR_HTTP_DATA_ERROR = 1004; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public final static int ERROR_TOO_MANY_REDIRECTS = 1005; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public final static int ERROR_INSUFFICIENT_SPACE = 1006; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public final static int ERROR_DEVICE_NOT_FOUND = 1007; |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public final static int ERROR_CANNOT_RESUME = 1008; |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public final static int ERROR_FILE_ALREADY_EXISTS = 1009; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public final static int ERROR_BLOCKED = 1010; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public final static int PAUSED_WAITING_TO_RETRY = 1; |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
public final static int PAUSED_WAITING_FOR_NETWORK = 2; |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
public final static int PAUSED_QUEUED_FOR_WIFI = 3; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public final static int PAUSED_UNKNOWN = 4; |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 308 |
public final static String ACTION_DOWNLOAD_COMPLETE = "android.intent.action.DOWNLOAD_COMPLETE"; |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
@SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 315 |
public final static String ACTION_NOTIFICATION_CLICKED = |
| 316 |
"android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"; |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 322 |
public final static String ACTION_VIEW_DOWNLOADS = "android.intent.action.VIEW_DOWNLOADS"; |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public final static String INTENT_EXTRAS_SORT_BY_SIZE = |
| 329 |
"android.app.DownloadManager.extra_sortBySize"; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public static final String EXTRA_DOWNLOAD_ID = "extra_download_id"; |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public static final String EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS = "extra_click_download_ids"; |
| 344 |
|
| 345 |
|
| 346 |
@SystemApi |
| 347 |
public static final String ACTION_DOWNLOAD_COMPLETED = |
| 348 |
"android.intent.action.DOWNLOAD_COMPLETED"; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 355 |
public static final String[] UNDERLYING_COLUMNS = new String[] { |
| 356 |
DownloadManager.COLUMN_ID, |
| 357 |
DownloadManager.COLUMN_LOCAL_FILENAME, |
| 358 |
DownloadManager.COLUMN_MEDIAPROVIDER_URI, |
| 359 |
DownloadManager.COLUMN_DESTINATION, |
| 360 |
DownloadManager.COLUMN_TITLE, |
| 361 |
DownloadManager.COLUMN_DESCRIPTION, |
| 362 |
DownloadManager.COLUMN_URI, |
| 363 |
DownloadManager.COLUMN_STATUS, |
| 364 |
DownloadManager.COLUMN_FILE_NAME_HINT, |
| 365 |
DownloadManager.COLUMN_MEDIA_TYPE, |
| 366 |
DownloadManager.COLUMN_TOTAL_SIZE_BYTES, |
| 367 |
DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP, |
| 368 |
DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR, |
| 369 |
DownloadManager.COLUMN_ALLOW_WRITE, |
| 370 |
DownloadManager.COLUMN_LOCAL_URI, |
| 371 |
DownloadManager.COLUMN_REASON |
| 372 |
}; |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
public static class Request { |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public static final int NETWORK_MOBILE = 1 << 0; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public static final int NETWORK_WIFI = 1 << 1; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
@Deprecated |
| 401 |
public static final int NETWORK_BLUETOOTH = 1 << 2; |
| 402 |
|
| 403 |
@UnsupportedAppUsage |
| 404 |
private Uri mUri; |
| 405 |
private Uri mDestinationUri; |
| 406 |
private List<Pair<String, String>> mRequestHeaders = new ArrayList<Pair<String, String>>(); |
| 407 |
private CharSequence mTitle; |
| 408 |
private CharSequence mDescription; |
| 409 |
private String mMimeType; |
| 410 |
private int mAllowedNetworkTypes = ~0; |
| 411 |
private boolean mRoamingAllowed = true; |
| 412 |
private boolean mMeteredAllowed = true; |
| 413 |
private int mFlags = 0; |
| 414 |
private boolean mIsVisibleInDownloadsUi = true; |
| 415 |
private boolean mScannable = false; |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
private static final int SCANNABLE_VALUE_YES = Downloads.Impl.MEDIA_NOT_SCANNED; |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
private static final int SCANNABLE_VALUE_NO = Downloads.Impl.MEDIA_NOT_SCANNABLE; |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public static final int VISIBILITY_VISIBLE = 0; |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
public static final int VISIBILITY_VISIBLE_NOTIFY_COMPLETED = 1; |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
public static final int VISIBILITY_HIDDEN = 2; |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public static final int VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION = 3; |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
private int mNotificationVisibility = VISIBILITY_VISIBLE; |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
public Request(Uri uri) { |
| 463 |
if (uri == null) { |
| 464 |
throw new NullPointerException(); |
| 465 |
} |
| 466 |
String scheme = uri.getScheme(); |
| 467 |
if (scheme == null || (!scheme.equals("http") && !scheme.equals("https"))) { |
| 468 |
throw new IllegalArgumentException("Can only download HTTP/HTTPS URIs: " + uri); |
| 469 |
} |
| 470 |
mUri = uri; |
| 471 |
} |
| 472 |
|
| 473 |
Request(String uriString) { |
| 474 |
mUri = Uri.parse(uriString); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
public Request setDestinationUri(Uri uri) { |
| 500 |
mDestinationUri = uri; |
| 501 |
return this; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
public Request setDestinationInExternalFilesDir(Context context, String dirType, |
| 523 |
String subPath) { |
| 524 |
final File file = context.getExternalFilesDir(dirType); |
| 525 |
if (file == null) { |
| 526 |
throw new IllegalStateException("Failed to get external storage files directory"); |
| 527 |
} else if (file.exists()) { |
| 528 |
if (!file.isDirectory()) { |
| 529 |
throw new IllegalStateException(file.getAbsolutePath() + |
| 530 |
" already exists and is not a directory"); |
| 531 |
} |
| 532 |
} else { |
| 533 |
if (!file.mkdirs()) { |
| 534 |
throw new IllegalStateException("Unable to create directory: "+ |
| 535 |
file.getAbsolutePath()); |
| 536 |
} |
| 537 |
} |
| 538 |
setDestinationFromBase(file, subPath); |
| 539 |
return this; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
public Request setDestinationInExternalPublicDir(String dirType, String subPath) { |
| 564 |
File file = Environment.getExternalStoragePublicDirectory(dirType); |
| 565 |
if (file == null) { |
| 566 |
throw new IllegalStateException("Failed to get external storage public directory"); |
| 567 |
} |
| 568 |
|
| 569 |
final Context context = AppGlobals.getInitialApplication(); |
| 570 |
if (context.getApplicationInfo().targetSdkVersion |
| 571 |
>= Build.VERSION_CODES.Q || !Environment.isExternalStorageLegacy()) { |
| 572 |
try (ContentProviderClient client = context.getContentResolver() |
| 573 |
.acquireContentProviderClient(Downloads.Impl.AUTHORITY)) { |
| 574 |
final Bundle extras = new Bundle(); |
| 575 |
extras.putString(Downloads.DIR_TYPE, dirType); |
| 576 |
client.call(Downloads.CALL_CREATE_EXTERNAL_PUBLIC_DIR, null, extras); |
| 577 |
} catch (RemoteException e) { |
| 578 |
throw new IllegalStateException("Unable to create directory: " |
| 579 |
+ file.getAbsolutePath()); |
| 580 |
} |
| 581 |
} else { |
| 582 |
if (file.exists()) { |
| 583 |
if (!file.isDirectory()) { |
| 584 |
throw new IllegalStateException(file.getAbsolutePath() |
| 585 |
+ " already exists and is not a directory"); |
| 586 |
} |
| 587 |
} else if (!file.mkdirs()) { |
| 588 |
throw new IllegalStateException("Unable to create directory: " |
| 589 |
+ file.getAbsolutePath()); |
| 590 |
} |
| 591 |
} |
| 592 |
setDestinationFromBase(file, subPath); |
| 593 |
return this; |
| 594 |
} |
| 595 |
|
| 596 |
private void setDestinationFromBase(File base, String subPath) { |
| 597 |
if (subPath == null) { |
| 598 |
throw new NullPointerException("subPath cannot be null"); |
| 599 |
} |
| 600 |
mDestinationUri = Uri.withAppendedPath(Uri.fromFile(base), subPath); |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
@Deprecated |
| 612 |
public void allowScanningByMediaScanner() { |
| 613 |
mScannable = true; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
public Request addRequestHeader(String header, String value) { |
| 626 |
if (header == null) { |
| 627 |
throw new NullPointerException("header cannot be null"); |
| 628 |
} |
| 629 |
if (header.contains(":")) { |
| 630 |
throw new IllegalArgumentException("header may not contain ':'"); |
| 631 |
} |
| 632 |
if (value == null) { |
| 633 |
value = ""; |
| 634 |
} |
| 635 |
mRequestHeaders.add(Pair.create(header, value)); |
| 636 |
return this; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
public Request setTitle(CharSequence title) { |
| 646 |
mTitle = title; |
| 647 |
return this; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
public Request setDescription(CharSequence description) { |
| 655 |
mDescription = description; |
| 656 |
return this; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
public Request setMimeType(String mimeType) { |
| 667 |
mMimeType = mimeType; |
| 668 |
return this; |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
@Deprecated |
| 685 |
public Request setShowRunningNotification(boolean show) { |
| 686 |
return (show) ? setNotificationVisibility(VISIBILITY_VISIBLE) : |
| 687 |
setNotificationVisibility(VISIBILITY_HIDDEN); |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
public Request setNotificationVisibility(int visibility) { |
| 708 |
mNotificationVisibility = visibility; |
| 709 |
return this; |
| 710 |
} |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
public Request setAllowedNetworkTypes(int flags) { |
| 726 |
mAllowedNetworkTypes = flags; |
| 727 |
return this; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
public Request setAllowedOverRoaming(boolean allowed) { |
| 737 |
mRoamingAllowed = allowed; |
| 738 |
return this; |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
public Request setAllowedOverMetered(boolean allow) { |
| 748 |
mMeteredAllowed = allow; |
| 749 |
return this; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
public Request setRequiresCharging(boolean requiresCharging) { |
| 760 |
if (requiresCharging) { |
| 761 |
mFlags |= Downloads.Impl.FLAG_REQUIRES_CHARGING; |
| 762 |
} else { |
| 763 |
mFlags &= ~Downloads.Impl.FLAG_REQUIRES_CHARGING; |
| 764 |
} |
| 765 |
return this; |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
public Request setRequiresDeviceIdle(boolean requiresDeviceIdle) { |
| 780 |
if (requiresDeviceIdle) { |
| 781 |
mFlags |= Downloads.Impl.FLAG_REQUIRES_DEVICE_IDLE; |
| 782 |
} else { |
| 783 |
mFlags &= ~Downloads.Impl.FLAG_REQUIRES_DEVICE_IDLE; |
| 784 |
} |
| 785 |
return this; |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
@Deprecated |
| 802 |
public Request setVisibleInDownloadsUi(boolean isVisible) { |
| 803 |
mIsVisibleInDownloadsUi = isVisible; |
| 804 |
return this; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
ContentValues toContentValues(String packageName) { |
| 811 |
ContentValues values = new ContentValues(); |
| 812 |
assert mUri != null; |
| 813 |
values.put(Downloads.Impl.COLUMN_URI, mUri.toString()); |
| 814 |
values.put(Downloads.Impl.COLUMN_IS_PUBLIC_API, true); |
| 815 |
values.put(Downloads.Impl.COLUMN_NOTIFICATION_PACKAGE, packageName); |
| 816 |
|
| 817 |
if (mDestinationUri != null) { |
| 818 |
values.put(Downloads.Impl.COLUMN_DESTINATION, |
| 819 |
Downloads.Impl.DESTINATION_FILE_URI); |
| 820 |
values.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, |
| 821 |
mDestinationUri.toString()); |
| 822 |
} else { |
| 823 |
values.put(Downloads.Impl.COLUMN_DESTINATION, |
| 824 |
Downloads.Impl.DESTINATION_CACHE_PARTITION_PURGEABLE); |
| 825 |
} |
| 826 |
|
| 827 |
values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED, (mScannable) ? SCANNABLE_VALUE_YES : |
| 828 |
SCANNABLE_VALUE_NO); |
| 829 |
|
| 830 |
if (!mRequestHeaders.isEmpty()) { |
| 831 |
encodeHttpHeaders(values); |
| 832 |
} |
| 833 |
|
| 834 |
putIfNonNull(values, Downloads.Impl.COLUMN_TITLE, mTitle); |
| 835 |
putIfNonNull(values, Downloads.Impl.COLUMN_DESCRIPTION, mDescription); |
| 836 |
putIfNonNull(values, Downloads.Impl.COLUMN_MIME_TYPE, mMimeType); |
| 837 |
|
| 838 |
values.put(Downloads.Impl.COLUMN_VISIBILITY, mNotificationVisibility); |
| 839 |
values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes); |
| 840 |
values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed); |
| 841 |
values.put(Downloads.Impl.COLUMN_ALLOW_METERED, mMeteredAllowed); |
| 842 |
values.put(Downloads.Impl.COLUMN_FLAGS, mFlags); |
| 843 |
values.put(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, mIsVisibleInDownloadsUi); |
| 844 |
|
| 845 |
return values; |
| 846 |
} |
| 847 |
|
| 848 |
private void encodeHttpHeaders(ContentValues values) { |
| 849 |
int index = 0; |
| 850 |
for (Pair<String, String> header : mRequestHeaders) { |
| 851 |
String headerString = header.first + ": " + header.second; |
| 852 |
values.put(Downloads.Impl.RequestHeaders.INSERT_KEY_PREFIX + index, headerString); |
| 853 |
index++; |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
private void putIfNonNull(ContentValues contentValues, String key, Object value) { |
| 858 |
if (value != null) { |
| 859 |
contentValues.put(key, value.toString()); |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
public static class Query { |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
public static final int ORDER_ASCENDING = 1; |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
public static final int ORDER_DESCENDING = 2; |
| 879 |
|
| 880 |
private long[] mIds = null; |
| 881 |
private Integer mStatusFlags = null; |
| 882 |
private String mFilterString = null; |
| 883 |
private String mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION; |
| 884 |
private int mOrderDirection = ORDER_DESCENDING; |
| 885 |
private boolean mOnlyIncludeVisibleInDownloadsUi = false; |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
public Query setFilterById(long... ids) { |
| 892 |
mIds = ids; |
| 893 |
return this; |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
public Query setFilterByString(@Nullable String filter) { |
| 903 |
mFilterString = filter; |
| 904 |
return this; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
public Query setFilterByStatus(int flags) { |
| 913 |
mStatusFlags = flags; |
| 914 |
return this; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
@UnsupportedAppUsage |
| 926 |
public Query setOnlyIncludeVisibleInDownloadsUi(boolean value) { |
| 927 |
mOnlyIncludeVisibleInDownloadsUi = value; |
| 928 |
return this; |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 942 |
public Query orderBy(String column, int direction) { |
| 943 |
if (direction != ORDER_ASCENDING && direction != ORDER_DESCENDING) { |
| 944 |
throw new IllegalArgumentException("Invalid direction: " + direction); |
| 945 |
} |
| 946 |
|
| 947 |
if (column.equals(COLUMN_LAST_MODIFIED_TIMESTAMP)) { |
| 948 |
mOrderByColumn = Downloads.Impl.COLUMN_LAST_MODIFICATION; |
| 949 |
} else if (column.equals(COLUMN_TOTAL_SIZE_BYTES)) { |
| 950 |
mOrderByColumn = Downloads.Impl.COLUMN_TOTAL_BYTES; |
| 951 |
} else { |
| 952 |
throw new IllegalArgumentException("Cannot order by " + column); |
| 953 |
} |
| 954 |
mOrderDirection = direction; |
| 955 |
return this; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
Cursor runQuery(ContentResolver resolver, String[] projection, Uri baseUri) { |
| 964 |
Uri uri = baseUri; |
| 965 |
List<String> selectionParts = new ArrayList<String>(); |
| 966 |
String[] selectionArgs = null; |
| 967 |
|
| 968 |
int whereArgsCount = (mIds == null) ? 0 : mIds.length; |
| 969 |
whereArgsCount = (mFilterString == null) ? whereArgsCount : whereArgsCount + 1; |
| 970 |
selectionArgs = new String[whereArgsCount]; |
| 971 |
|
| 972 |
if (whereArgsCount > 0) { |
| 973 |
if (mIds != null) { |
| 974 |
selectionParts.add(getWhereClauseForIds(mIds)); |
| 975 |
getWhereArgsForIds(mIds, selectionArgs); |
| 976 |
} |
| 977 |
|
| 978 |
if (mFilterString != null) { |
| 979 |
selectionParts.add(Downloads.Impl.COLUMN_TITLE + " LIKE ?"); |
| 980 |
selectionArgs[selectionArgs.length - 1] = "%" + mFilterString + "%"; |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
if (mStatusFlags != null) { |
| 985 |
List<String> parts = new ArrayList<String>(); |
| 986 |
if ((mStatusFlags & STATUS_PENDING) != 0) { |
| 987 |
parts.add(statusClause("=", Downloads.Impl.STATUS_PENDING)); |
| 988 |
} |
| 989 |
if ((mStatusFlags & STATUS_RUNNING) != 0) { |
| 990 |
parts.add(statusClause("=", Downloads.Impl.STATUS_RUNNING)); |
| 991 |
} |
| 992 |
if ((mStatusFlags & STATUS_PAUSED) != 0) { |
| 993 |
parts.add(statusClause("=", Downloads.Impl.STATUS_PAUSED_BY_APP)); |
| 994 |
parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_TO_RETRY)); |
| 995 |
parts.add(statusClause("=", Downloads.Impl.STATUS_WAITING_FOR_NETWORK)); |
| 996 |
parts.add(statusClause("=", Downloads.Impl.STATUS_QUEUED_FOR_WIFI)); |
| 997 |
} |
| 998 |
if ((mStatusFlags & STATUS_SUCCESSFUL) != 0) { |
| 999 |
parts.add(statusClause("=", Downloads.Impl.STATUS_SUCCESS)); |
| 1000 |
} |
| 1001 |
if ((mStatusFlags & STATUS_FAILED) != 0) { |
| 1002 |
parts.add("(" + statusClause(">=", 400) |
| 1003 |
+ " AND " + statusClause("<", 600) + ")"); |
| 1004 |
} |
| 1005 |
selectionParts.add(joinStrings(" OR ", parts)); |
| 1006 |
} |
| 1007 |
|
| 1008 |
if (mOnlyIncludeVisibleInDownloadsUi) { |
| 1009 |
selectionParts.add(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI + " != '0'"); |
| 1010 |
} |
| 1011 |
|
| 1012 |
|
| 1013 |
selectionParts.add(Downloads.Impl.COLUMN_DELETED + " != '1'"); |
| 1014 |
|
| 1015 |
String selection = joinStrings(" AND ", selectionParts); |
| 1016 |
String orderDirection = (mOrderDirection == ORDER_ASCENDING ? "ASC" : "DESC"); |
| 1017 |
String orderBy = mOrderByColumn + " " + orderDirection; |
| 1018 |
|
| 1019 |
return resolver.query(uri, projection, selection, selectionArgs, orderBy); |
| 1020 |
} |
| 1021 |
|
| 1022 |
private String joinStrings(String joiner, Iterable<String> parts) { |
| 1023 |
StringBuilder builder = new StringBuilder(); |
| 1024 |
boolean first = true; |
| 1025 |
for (String part : parts) { |
| 1026 |
if (!first) { |
| 1027 |
builder.append(joiner); |
| 1028 |
} |
| 1029 |
builder.append(part); |
| 1030 |
first = false; |
| 1031 |
} |
| 1032 |
return builder.toString(); |
| 1033 |
} |
| 1034 |
|
| 1035 |
private String statusClause(String operator, int value) { |
| 1036 |
return Downloads.Impl.COLUMN_STATUS + operator + "'" + value + "'"; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
private final ContentResolver mResolver; |
| 1041 |
private final String mPackageName; |
| 1042 |
|
| 1043 |
private Uri mBaseUri = Downloads.Impl.CONTENT_URI; |
| 1044 |
private boolean mAccessFilename; |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
public DownloadManager(Context context) { |
| 1050 |
mResolver = context.getContentResolver(); |
| 1051 |
mPackageName = context.getPackageName(); |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
mAccessFilename = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N; |
| 1056 |
} |
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
@UnsupportedAppUsage |
| 1064 |
public void setAccessAllDownloads(boolean accessAllDownloads) { |
| 1065 |
if (accessAllDownloads) { |
| 1066 |
mBaseUri = Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI; |
| 1067 |
} else { |
| 1068 |
mBaseUri = Downloads.Impl.CONTENT_URI; |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 1074 |
public void setAccessFilename(boolean accessFilename) { |
| 1075 |
mAccessFilename = accessFilename; |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
@SystemApi |
| 1088 |
@RequiresPermission(android.Manifest.permission.WRITE_MEDIA_STORAGE) |
| 1089 |
public void onMediaStoreDownloadsDeleted(@NonNull LongSparseArray<String> idToMime) { |
| 1090 |
try (ContentProviderClient client = mResolver |
| 1091 |
.acquireUnstableContentProviderClient(mBaseUri)) { |
| 1092 |
final Bundle callExtras = new Bundle(); |
| 1093 |
final long[] ids = new long[idToMime.size()]; |
| 1094 |
final String[] mimeTypes = new String[idToMime.size()]; |
| 1095 |
for (int i = idToMime.size() - 1; i >= 0; --i) { |
| 1096 |
ids[i] = idToMime.keyAt(i); |
| 1097 |
mimeTypes[i] = idToMime.valueAt(i); |
| 1098 |
} |
| 1099 |
callExtras.putLongArray(android.provider.Downloads.EXTRA_IDS, ids); |
| 1100 |
callExtras.putStringArray(android.provider.Downloads.EXTRA_MIME_TYPES, |
| 1101 |
mimeTypes); |
| 1102 |
client.call(android.provider.Downloads.CALL_MEDIASTORE_DOWNLOADS_DELETED, |
| 1103 |
null, callExtras); |
| 1104 |
} catch (RemoteException e) { |
| 1105 |
|
| 1106 |
} |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
public long enqueue(Request request) { |
| 1118 |
ContentValues values = request.toContentValues(mPackageName); |
| 1119 |
Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values); |
| 1120 |
long id = Long.parseLong(downloadUri.getLastPathSegment()); |
| 1121 |
return id; |
| 1122 |
} |
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
public int markRowDeleted(long... ids) { |
| 1134 |
if (ids == null || ids.length == 0) { |
| 1135 |
|
| 1136 |
throw new IllegalArgumentException("input param 'ids' can't be null"); |
| 1137 |
} |
| 1138 |
return mResolver.delete(mBaseUri, getWhereClauseForIds(ids), getWhereArgsForIds(ids)); |
| 1139 |
} |
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
public int remove(long... ids) { |
| 1150 |
return markRowDeleted(ids); |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
public Cursor query(Query query) { |
| 1160 |
return query(query, UNDERLYING_COLUMNS); |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
public Cursor query(Query query, String[] projection) { |
| 1165 |
Cursor underlyingCursor = query.runQuery(mResolver, projection, mBaseUri); |
| 1166 |
if (underlyingCursor == null) { |
| 1167 |
return null; |
| 1168 |
} |
| 1169 |
return new CursorTranslator(underlyingCursor, mBaseUri, mAccessFilename); |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
public ParcelFileDescriptor openDownloadedFile(long id) throws FileNotFoundException { |
| 1179 |
return mResolver.openFileDescriptor(getDownloadUri(id), "r"); |
| 1180 |
} |
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
public Uri getUriForDownloadedFile(long id) { |
| 1191 |
|
| 1192 |
Query query = new Query().setFilterById(id); |
| 1193 |
Cursor cursor = null; |
| 1194 |
try { |
| 1195 |
cursor = query(query); |
| 1196 |
if (cursor == null) { |
| 1197 |
return null; |
| 1198 |
} |
| 1199 |
if (cursor.moveToFirst()) { |
| 1200 |
int status = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_STATUS)); |
| 1201 |
if (DownloadManager.STATUS_SUCCESSFUL == status) { |
| 1202 |
return ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, id); |
| 1203 |
} |
| 1204 |
} |
| 1205 |
} finally { |
| 1206 |
if (cursor != null) { |
| 1207 |
cursor.close(); |
| 1208 |
} |
| 1209 |
} |
| 1210 |
|
| 1211 |
return null; |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
public String getMimeTypeForDownloadedFile(long id) { |
| 1223 |
Query query = new Query().setFilterById(id); |
| 1224 |
Cursor cursor = null; |
| 1225 |
try { |
| 1226 |
cursor = query(query); |
| 1227 |
if (cursor == null) { |
| 1228 |
return null; |
| 1229 |
} |
| 1230 |
while (cursor.moveToFirst()) { |
| 1231 |
return cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_MEDIA_TYPE)); |
| 1232 |
} |
| 1233 |
} finally { |
| 1234 |
if (cursor != null) { |
| 1235 |
cursor.close(); |
| 1236 |
} |
| 1237 |
} |
| 1238 |
|
| 1239 |
return null; |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
@UnsupportedAppUsage |
| 1249 |
public void restartDownload(long... ids) { |
| 1250 |
Cursor cursor = query(new Query().setFilterById(ids)); |
| 1251 |
try { |
| 1252 |
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { |
| 1253 |
int status = cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS)); |
| 1254 |
if (status != STATUS_SUCCESSFUL && status != STATUS_FAILED) { |
| 1255 |
throw new IllegalArgumentException("Cannot restart incomplete download: " |
| 1256 |
+ cursor.getLong(cursor.getColumnIndex(COLUMN_ID))); |
| 1257 |
} |
| 1258 |
} |
| 1259 |
} finally { |
| 1260 |
cursor.close(); |
| 1261 |
} |
| 1262 |
|
| 1263 |
ContentValues values = new ContentValues(); |
| 1264 |
values.put(Downloads.Impl.COLUMN_CURRENT_BYTES, 0); |
| 1265 |
values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, -1); |
| 1266 |
values.putNull(Downloads.Impl._DATA); |
| 1267 |
values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING); |
| 1268 |
values.put(Downloads.Impl.COLUMN_FAILED_CONNECTIONS, 0); |
| 1269 |
mResolver.update(mBaseUri, values, getWhereClauseForIds(ids), getWhereArgsForIds(ids)); |
| 1270 |
} |
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
public void forceDownload(long... ids) { |
| 1279 |
ContentValues values = new ContentValues(); |
| 1280 |
values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_PENDING); |
| 1281 |
values.put(Downloads.Impl.COLUMN_CONTROL, Downloads.Impl.CONTROL_RUN); |
| 1282 |
values.put(Downloads.Impl.COLUMN_BYPASS_RECOMMENDED_SIZE_LIMIT, 1); |
| 1283 |
mResolver.update(mBaseUri, values, getWhereClauseForIds(ids), getWhereArgsForIds(ids)); |
| 1284 |
} |
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
public static Long getMaxBytesOverMobile(Context context) { |
| 1295 |
try { |
| 1296 |
return Settings.Global.getLong(context.getContentResolver(), |
| 1297 |
Settings.Global.DOWNLOAD_MAX_BYTES_OVER_MOBILE); |
| 1298 |
} catch (SettingNotFoundException exc) { |
| 1299 |
return null; |
| 1300 |
} |
| 1301 |
} |
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
public boolean rename(Context context, long id, String displayName) { |
| 1313 |
if (!FileUtils.isValidFatFilename(displayName)) { |
| 1314 |
throw new SecurityException(displayName + " is not a valid filename"); |
| 1315 |
} |
| 1316 |
|
| 1317 |
final String filePath; |
| 1318 |
final Query query = new Query().setFilterById(id); |
| 1319 |
try (Cursor cursor = query(query)) { |
| 1320 |
if (cursor == null) { |
| 1321 |
throw new IllegalStateException("Missing cursor for download id=" + id); |
| 1322 |
} |
| 1323 |
if (cursor.moveToFirst()) { |
| 1324 |
final int status = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_STATUS)); |
| 1325 |
if (status != DownloadManager.STATUS_SUCCESSFUL) { |
| 1326 |
throw new IllegalStateException("Download is not completed yet: " |
| 1327 |
+ DatabaseUtils.dumpCurrentRowToString(cursor)); |
| 1328 |
} |
| 1329 |
filePath = cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_LOCAL_FILENAME)); |
| 1330 |
if (filePath == null) { |
| 1331 |
throw new IllegalStateException("Download doesn't have a valid file path: " |
| 1332 |
+ DatabaseUtils.dumpCurrentRowToString(cursor)); |
| 1333 |
} else if (!new File(filePath).exists()) { |
| 1334 |
throw new IllegalStateException("Downloaded file doesn't exist anymore: " |
| 1335 |
+ DatabaseUtils.dumpCurrentRowToString(cursor)); |
| 1336 |
} |
| 1337 |
} else { |
| 1338 |
throw new IllegalStateException("Missing download id=" + id); |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
final File before = new File(filePath); |
| 1343 |
final File after = new File(before.getParentFile(), displayName); |
| 1344 |
|
| 1345 |
if (after.exists()) { |
| 1346 |
throw new IllegalStateException("File already exists: " + after); |
| 1347 |
} |
| 1348 |
if (!before.renameTo(after)) { |
| 1349 |
throw new IllegalStateException( |
| 1350 |
"Failed to rename file from " + before + " to " + after); |
| 1351 |
} |
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
MediaStore.scanFile(mResolver, before); |
| 1356 |
MediaStore.scanFile(mResolver, after); |
| 1357 |
|
| 1358 |
final ContentValues values = new ContentValues(); |
| 1359 |
values.put(Downloads.Impl.COLUMN_TITLE, displayName); |
| 1360 |
values.put(Downloads.Impl._DATA, after.toString()); |
| 1361 |
values.putNull(Downloads.Impl.COLUMN_MEDIAPROVIDER_URI); |
| 1362 |
final long[] ids = { id }; |
| 1363 |
|
| 1364 |
return mResolver.update( |
| 1365 |
mBaseUri, values, getWhereClauseForIds(ids), getWhereArgsForIds(ids)) == 1; |
| 1366 |
} |
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
public static Long getRecommendedMaxBytesOverMobile(Context context) { |
| 1378 |
try { |
| 1379 |
return Settings.Global.getLong(context.getContentResolver(), |
| 1380 |
Settings.Global.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE); |
| 1381 |
} catch (SettingNotFoundException exc) { |
| 1382 |
return null; |
| 1383 |
} |
| 1384 |
} |
| 1385 |
|
| 1386 |
|
| 1387 |
public static boolean isActiveNetworkExpensive(Context context) { |
| 1388 |
|
| 1389 |
return false; |
| 1390 |
} |
| 1391 |
|
| 1392 |
|
| 1393 |
public static long getActiveNetworkWarningBytes(Context context) { |
| 1394 |
|
| 1395 |
return -1; |
| 1396 |
} |
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
@Deprecated |
| 1435 |
public long addCompletedDownload(String title, String description, |
| 1436 |
boolean isMediaScannerScannable, String mimeType, String path, long length, |
| 1437 |
boolean showNotification) { |
| 1438 |
return addCompletedDownload(title, description, isMediaScannerScannable, mimeType, path, |
| 1439 |
length, showNotification, false, null, null); |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
@Deprecated |
| 1481 |
public long addCompletedDownload(String title, String description, |
| 1482 |
boolean isMediaScannerScannable, String mimeType, String path, long length, |
| 1483 |
boolean showNotification, Uri uri, Uri referer) { |
| 1484 |
return addCompletedDownload(title, description, isMediaScannerScannable, mimeType, path, |
| 1485 |
length, showNotification, false, uri, referer); |
| 1486 |
} |
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
@Deprecated |
| 1506 |
public long addCompletedDownload(String title, String description, |
| 1507 |
boolean isMediaScannerScannable, String mimeType, String path, long length, |
| 1508 |
boolean showNotification, boolean allowWrite) { |
| 1509 |
return addCompletedDownload(title, description, isMediaScannerScannable, mimeType, path, |
| 1510 |
length, showNotification, allowWrite, null, null); |
| 1511 |
} |
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
@Deprecated |
| 1531 |
public long addCompletedDownload(String title, String description, |
| 1532 |
boolean isMediaScannerScannable, String mimeType, String path, long length, |
| 1533 |
boolean showNotification, boolean allowWrite, Uri uri, Uri referer) { |
| 1534 |
|
| 1535 |
validateArgumentIsNonEmpty("title", title); |
| 1536 |
validateArgumentIsNonEmpty("description", description); |
| 1537 |
validateArgumentIsNonEmpty("path", path); |
| 1538 |
validateArgumentIsNonEmpty("mimeType", mimeType); |
| 1539 |
if (length < 0) { |
| 1540 |
throw new IllegalArgumentException(" invalid value for param: totalBytes"); |
| 1541 |
} |
| 1542 |
|
| 1543 |
|
| 1544 |
Request request; |
| 1545 |
if (uri != null) { |
| 1546 |
request = new Request(uri); |
| 1547 |
} else { |
| 1548 |
request = new Request(NON_DOWNLOADMANAGER_DOWNLOAD); |
| 1549 |
} |
| 1550 |
request.setTitle(title) |
| 1551 |
.setDescription(description) |
| 1552 |
.setMimeType(mimeType); |
| 1553 |
if (referer != null) { |
| 1554 |
request.addRequestHeader("Referer", referer.toString()); |
| 1555 |
} |
| 1556 |
ContentValues values = request.toContentValues(null); |
| 1557 |
values.put(Downloads.Impl.COLUMN_DESTINATION, |
| 1558 |
Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD); |
| 1559 |
values.put(Downloads.Impl._DATA, path); |
| 1560 |
values.put(Downloads.Impl.COLUMN_MIME_TYPE, resolveMimeType(new File(path))); |
| 1561 |
values.put(Downloads.Impl.COLUMN_STATUS, Downloads.Impl.STATUS_SUCCESS); |
| 1562 |
values.put(Downloads.Impl.COLUMN_TOTAL_BYTES, length); |
| 1563 |
values.put(Downloads.Impl.COLUMN_MEDIA_SCANNED, |
| 1564 |
(isMediaScannerScannable) ? Request.SCANNABLE_VALUE_YES : |
| 1565 |
Request.SCANNABLE_VALUE_NO); |
| 1566 |
values.put(Downloads.Impl.COLUMN_VISIBILITY, (showNotification) ? |
| 1567 |
Request.VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION : Request.VISIBILITY_HIDDEN); |
| 1568 |
values.put(Downloads.Impl.COLUMN_ALLOW_WRITE, allowWrite ? 1 : 0); |
| 1569 |
Uri downloadUri = mResolver.insert(Downloads.Impl.CONTENT_URI, values); |
| 1570 |
if (downloadUri == null) { |
| 1571 |
return -1; |
| 1572 |
} |
| 1573 |
return Long.parseLong(downloadUri.getLastPathSegment()); |
| 1574 |
} |
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
private static @NonNull String resolveMimeType(@NonNull File file) { |
| 1583 |
final String extension = extractFileExtension(file.getPath()); |
| 1584 |
if (extension == null) return ClipDescription.MIMETYPE_UNKNOWN; |
| 1585 |
|
| 1586 |
final String mimeType = MimeTypeMap.getSingleton() |
| 1587 |
.getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT)); |
| 1588 |
if (mimeType == null) return ClipDescription.MIMETYPE_UNKNOWN; |
| 1589 |
|
| 1590 |
return mimeType; |
| 1591 |
} |
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
private static @Nullable String extractDisplayName(@Nullable String data) { |
| 1600 |
if (data == null) return null; |
| 1601 |
if (data.indexOf('/') == -1) { |
| 1602 |
return data; |
| 1603 |
} |
| 1604 |
if (data.endsWith("/")) { |
| 1605 |
data = data.substring(0, data.length() - 1); |
| 1606 |
} |
| 1607 |
return data.substring(data.lastIndexOf('/') + 1); |
| 1608 |
} |
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
private static @Nullable String extractFileExtension(@Nullable String data) { |
| 1617 |
if (data == null) return null; |
| 1618 |
data = extractDisplayName(data); |
| 1619 |
|
| 1620 |
final int lastDot = data.lastIndexOf('.'); |
| 1621 |
if (lastDot == -1) { |
| 1622 |
return null; |
| 1623 |
} else { |
| 1624 |
return data.substring(lastDot + 1); |
| 1625 |
} |
| 1626 |
} |
| 1627 |
|
| 1628 |
private static final String NON_DOWNLOADMANAGER_DOWNLOAD = |
| 1629 |
"non-dwnldmngr-download-dont-retry2download"; |
| 1630 |
|
| 1631 |
private static void validateArgumentIsNonEmpty(String paramName, String val) { |
| 1632 |
if (TextUtils.isEmpty(val)) { |
| 1633 |
throw new IllegalArgumentException(paramName + " can't be null"); |
| 1634 |
} |
| 1635 |
} |
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
public Uri getDownloadUri(long id) { |
| 1643 |
return ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, id); |
| 1644 |
} |
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1650 |
static String getWhereClauseForIds(long[] ids) { |
| 1651 |
StringBuilder whereClause = new StringBuilder(); |
| 1652 |
whereClause.append("("); |
| 1653 |
for (int i = 0; i < ids.length; i++) { |
| 1654 |
if (i > 0) { |
| 1655 |
whereClause.append("OR "); |
| 1656 |
} |
| 1657 |
whereClause.append(Downloads.Impl._ID); |
| 1658 |
whereClause.append(" = ? "); |
| 1659 |
} |
| 1660 |
whereClause.append(")"); |
| 1661 |
return whereClause.toString(); |
| 1662 |
} |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1668 |
static String[] getWhereArgsForIds(long[] ids) { |
| 1669 |
String[] whereArgs = new String[ids.length]; |
| 1670 |
return getWhereArgsForIds(ids, whereArgs); |
| 1671 |
} |
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
static String[] getWhereArgsForIds(long[] ids, String[] args) { |
| 1678 |
assert(args.length >= ids.length); |
| 1679 |
for (int i = 0; i < ids.length; i++) { |
| 1680 |
args[i] = Long.toString(ids[i]); |
| 1681 |
} |
| 1682 |
return args; |
| 1683 |
} |
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
private static class CursorTranslator extends CursorWrapper { |
| 1693 |
private final Uri mBaseUri; |
| 1694 |
private final boolean mAccessFilename; |
| 1695 |
|
| 1696 |
public CursorTranslator(Cursor cursor, Uri baseUri, boolean accessFilename) { |
| 1697 |
super(cursor); |
| 1698 |
mBaseUri = baseUri; |
| 1699 |
mAccessFilename = accessFilename; |
| 1700 |
} |
| 1701 |
|
| 1702 |
@Override |
| 1703 |
public int getInt(int columnIndex) { |
| 1704 |
return (int) getLong(columnIndex); |
| 1705 |
} |
| 1706 |
|
| 1707 |
@Override |
| 1708 |
public long getLong(int columnIndex) { |
| 1709 |
if (getColumnName(columnIndex).equals(COLUMN_REASON)) { |
| 1710 |
return getReason(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS))); |
| 1711 |
} else if (getColumnName(columnIndex).equals(COLUMN_STATUS)) { |
| 1712 |
return translateStatus(super.getInt(getColumnIndex(Downloads.Impl.COLUMN_STATUS))); |
| 1713 |
} else { |
| 1714 |
return super.getLong(columnIndex); |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
@Override |
| 1719 |
public String getString(int columnIndex) { |
| 1720 |
final String columnName = getColumnName(columnIndex); |
| 1721 |
switch (columnName) { |
| 1722 |
case COLUMN_LOCAL_URI: |
| 1723 |
return getLocalUri(); |
| 1724 |
case COLUMN_LOCAL_FILENAME: |
| 1725 |
if (!mAccessFilename) { |
| 1726 |
throw new SecurityException( |
| 1727 |
"COLUMN_LOCAL_FILENAME is deprecated;" |
| 1728 |
+ " use ContentResolver.openFileDescriptor() instead"); |
| 1729 |
} |
| 1730 |
default: |
| 1731 |
return super.getString(columnIndex); |
| 1732 |
} |
| 1733 |
} |
| 1734 |
|
| 1735 |
private String getLocalUri() { |
| 1736 |
long destinationType = getLong(getColumnIndex(Downloads.Impl.COLUMN_DESTINATION)); |
| 1737 |
if (destinationType == Downloads.Impl.DESTINATION_FILE_URI || |
| 1738 |
destinationType == Downloads.Impl.DESTINATION_EXTERNAL || |
| 1739 |
destinationType == Downloads.Impl.DESTINATION_NON_DOWNLOADMANAGER_DOWNLOAD) { |
| 1740 |
String localPath = super.getString(getColumnIndex(COLUMN_LOCAL_FILENAME)); |
| 1741 |
if (localPath == null) { |
| 1742 |
return null; |
| 1743 |
} |
| 1744 |
return Uri.fromFile(new File(localPath)).toString(); |
| 1745 |
} |
| 1746 |
|
| 1747 |
|
| 1748 |
long downloadId = getLong(getColumnIndex(Downloads.Impl._ID)); |
| 1749 |
return ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, downloadId).toString(); |
| 1750 |
} |
| 1751 |
|
| 1752 |
private long getReason(int status) { |
| 1753 |
switch (translateStatus(status)) { |
| 1754 |
case STATUS_FAILED: |
| 1755 |
return getErrorCode(status); |
| 1756 |
|
| 1757 |
case STATUS_PAUSED: |
| 1758 |
return getPausedReason(status); |
| 1759 |
|
| 1760 |
default: |
| 1761 |
return 0; |
| 1762 |
} |
| 1763 |
} |
| 1764 |
|
| 1765 |
private long getPausedReason(int status) { |
| 1766 |
switch (status) { |
| 1767 |
case Downloads.Impl.STATUS_WAITING_TO_RETRY: |
| 1768 |
return PAUSED_WAITING_TO_RETRY; |
| 1769 |
|
| 1770 |
case Downloads.Impl.STATUS_WAITING_FOR_NETWORK: |
| 1771 |
return PAUSED_WAITING_FOR_NETWORK; |
| 1772 |
|
| 1773 |
case Downloads.Impl.STATUS_QUEUED_FOR_WIFI: |
| 1774 |
return PAUSED_QUEUED_FOR_WIFI; |
| 1775 |
|
| 1776 |
default: |
| 1777 |
return PAUSED_UNKNOWN; |
| 1778 |
} |
| 1779 |
} |
| 1780 |
|
| 1781 |
private long getErrorCode(int status) { |
| 1782 |
if ((400 <= status && status < Downloads.Impl.MIN_ARTIFICIAL_ERROR_STATUS) |
| 1783 |
|| (500 <= status && status < 600)) { |
| 1784 |
|
| 1785 |
return status; |
| 1786 |
} |
| 1787 |
|
| 1788 |
switch (status) { |
| 1789 |
case Downloads.Impl.STATUS_FILE_ERROR: |
| 1790 |
return ERROR_FILE_ERROR; |
| 1791 |
|
| 1792 |
case Downloads.Impl.STATUS_UNHANDLED_HTTP_CODE: |
| 1793 |
case Downloads.Impl.STATUS_UNHANDLED_REDIRECT: |
| 1794 |
return ERROR_UNHANDLED_HTTP_CODE; |
| 1795 |
|
| 1796 |
case Downloads.Impl.STATUS_HTTP_DATA_ERROR: |
| 1797 |
return ERROR_HTTP_DATA_ERROR; |
| 1798 |
|
| 1799 |
case Downloads.Impl.STATUS_TOO_MANY_REDIRECTS: |
| 1800 |
return ERROR_TOO_MANY_REDIRECTS; |
| 1801 |
|
| 1802 |
case Downloads.Impl.STATUS_INSUFFICIENT_SPACE_ERROR: |
| 1803 |
return ERROR_INSUFFICIENT_SPACE; |
| 1804 |
|
| 1805 |
case Downloads.Impl.STATUS_DEVICE_NOT_FOUND_ERROR: |
| 1806 |
return ERROR_DEVICE_NOT_FOUND; |
| 1807 |
|
| 1808 |
case Downloads.Impl.STATUS_CANNOT_RESUME: |
| 1809 |
return ERROR_CANNOT_RESUME; |
| 1810 |
|
| 1811 |
case Downloads.Impl.STATUS_FILE_ALREADY_EXISTS_ERROR: |
| 1812 |
return ERROR_FILE_ALREADY_EXISTS; |
| 1813 |
|
| 1814 |
default: |
| 1815 |
return ERROR_UNKNOWN; |
| 1816 |
} |
| 1817 |
} |
| 1818 |
|
| 1819 |
private int translateStatus(int status) { |
| 1820 |
switch (status) { |
| 1821 |
case Downloads.Impl.STATUS_PENDING: |
| 1822 |
return STATUS_PENDING; |
| 1823 |
|
| 1824 |
case Downloads.Impl.STATUS_RUNNING: |
| 1825 |
return STATUS_RUNNING; |
| 1826 |
|
| 1827 |
case Downloads.Impl.STATUS_PAUSED_BY_APP: |
| 1828 |
case Downloads.Impl.STATUS_WAITING_TO_RETRY: |
| 1829 |
case Downloads.Impl.STATUS_WAITING_FOR_NETWORK: |
| 1830 |
case Downloads.Impl.STATUS_QUEUED_FOR_WIFI: |
| 1831 |
return STATUS_PAUSED; |
| 1832 |
|
| 1833 |
case Downloads.Impl.STATUS_SUCCESS: |
| 1834 |
return STATUS_SUCCESSFUL; |
| 1835 |
|
| 1836 |
default: |
| 1837 |
assert Downloads.Impl.isStatusError(status); |
| 1838 |
return STATUS_FAILED; |
| 1839 |
} |
| 1840 |
} |
| 1841 |
} |
| 1842 |
} |