| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.annotation.SystemApi; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.Intent; |
| 24 |
import android.os.Environment; |
| 25 |
import android.os.Parcel; |
| 26 |
import android.os.Parcelable; |
| 27 |
import android.os.StrictMode; |
| 28 |
import android.util.Log; |
| 29 |
|
| 30 |
import java.io.File; |
| 31 |
import java.io.IOException; |
| 32 |
import java.io.UnsupportedEncodingException; |
| 33 |
import java.net.URLEncoder; |
| 34 |
import java.nio.charset.StandardCharsets; |
| 35 |
import java.util.AbstractList; |
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.Collections; |
| 38 |
import java.util.LinkedHashSet; |
| 39 |
import java.util.List; |
| 40 |
import java.util.Locale; |
| 41 |
import java.util.Objects; |
| 42 |
import java.util.RandomAccess; |
| 43 |
import java.util.Set; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public abstract class Uri implements Parcelable, Comparable<Uri> { |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
private static final String LOG = Uri.class.getSimpleName(); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
static class NotCachedHolder { |
| 134 |
private NotCachedHolder() { |
| 135 |
|
| 136 |
} |
| 137 |
@SuppressWarnings("RedundantStringConstructorCall") |
| 138 |
static final String NOT_CACHED = new String("NOT CACHED"); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public static final Uri EMPTY = new HierarchicalUri(null, Part.NULL, |
| 145 |
PathPart.EMPTY, Part.NULL, Part.NULL); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
@UnsupportedAppUsage |
| 151 |
private Uri() {} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public abstract boolean isHierarchical(); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public boolean isOpaque() { |
| 165 |
return !isHierarchical(); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public abstract boolean isRelative(); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public boolean isAbsolute() { |
| 183 |
return !isRelative(); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
@Nullable |
| 192 |
public abstract String getScheme(); |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public abstract String getSchemeSpecificPart(); |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public abstract String getEncodedSchemeSpecificPart(); |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
@Nullable |
| 227 |
public abstract String getAuthority(); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
@Nullable |
| 239 |
public abstract String getEncodedAuthority(); |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
@Nullable |
| 249 |
public abstract String getUserInfo(); |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
@Nullable |
| 259 |
public abstract String getEncodedUserInfo(); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
@Nullable |
| 269 |
public abstract String getHost(); |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public abstract int getPort(); |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
@Nullable |
| 286 |
public abstract String getPath(); |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
@Nullable |
| 295 |
public abstract String getEncodedPath(); |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
@Nullable |
| 306 |
public abstract String getQuery(); |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
@Nullable |
| 317 |
public abstract String getEncodedQuery(); |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
@Nullable |
| 325 |
public abstract String getFragment(); |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
@Nullable |
| 333 |
public abstract String getEncodedFragment(); |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public abstract List<String> getPathSegments(); |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
@Nullable |
| 348 |
public abstract String getLastPathSegment(); |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
public boolean equals(@Nullable Object o) { |
| 358 |
if (!(o instanceof Uri)) { |
| 359 |
return false; |
| 360 |
} |
| 361 |
|
| 362 |
Uri other = (Uri) o; |
| 363 |
|
| 364 |
return toString().equals(other.toString()); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public int hashCode() { |
| 372 |
return toString().hashCode(); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
public int compareTo(Uri other) { |
| 380 |
return toString().compareTo(other.toString()); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public abstract String toString(); |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
@SystemApi |
| 399 |
public @NonNull String toSafeString() { |
| 400 |
String scheme = getScheme(); |
| 401 |
String ssp = getSchemeSpecificPart(); |
| 402 |
StringBuilder builder = new StringBuilder(64); |
| 403 |
|
| 404 |
if (scheme != null) { |
| 405 |
builder.append(scheme); |
| 406 |
builder.append(":"); |
| 407 |
if (scheme.equalsIgnoreCase("tel") || scheme.equalsIgnoreCase("sip") |
| 408 |
|| scheme.equalsIgnoreCase("sms") || scheme.equalsIgnoreCase("smsto") |
| 409 |
|| scheme.equalsIgnoreCase("mailto") || scheme.equalsIgnoreCase("nfc")) { |
| 410 |
if (ssp != null) { |
| 411 |
for (int i=0; i<ssp.length(); i++) { |
| 412 |
char c = ssp.charAt(i); |
| 413 |
if (c == '-' || c == '@' || c == '.') { |
| 414 |
builder.append(c); |
| 415 |
} else { |
| 416 |
builder.append('x'); |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
} else { |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
final String host = getHost(); |
| 425 |
final int port = getPort(); |
| 426 |
final String path = getPath(); |
| 427 |
final String authority = getAuthority(); |
| 428 |
if (authority != null) builder.append("//"); |
| 429 |
if (host != null) builder.append(host); |
| 430 |
if (port != -1) builder.append(":").append(port); |
| 431 |
if (authority != null || path != null) builder.append("/..."); |
| 432 |
} |
| 433 |
} |
| 434 |
return builder.toString(); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
public abstract Builder buildUpon(); |
| 441 |
|
| 442 |
|
| 443 |
private final static int NOT_FOUND = -1; |
| 444 |
|
| 445 |
|
| 446 |
private final static int NOT_CALCULATED = -2; |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
private static final String NOT_HIERARCHICAL |
| 453 |
= "This isn't a hierarchical URI."; |
| 454 |
|
| 455 |
|
| 456 |
private static final String DEFAULT_ENCODING = "UTF-8"; |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public static Uri parse(String uriString) { |
| 466 |
return new StringUri(uriString); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
public static Uri fromFile(File file) { |
| 480 |
if (file == null) { |
| 481 |
throw new NullPointerException("file"); |
| 482 |
} |
| 483 |
|
| 484 |
PathPart path = PathPart.fromDecoded(file.getAbsolutePath()); |
| 485 |
return new HierarchicalUri( |
| 486 |
"file", Part.EMPTY, path, Part.NULL, Part.NULL); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
private static class StringUri extends AbstractHierarchicalUri { |
| 495 |
|
| 496 |
|
| 497 |
static final int TYPE_ID = 1; |
| 498 |
|
| 499 |
|
| 500 |
private final String uriString; |
| 501 |
|
| 502 |
private StringUri(String uriString) { |
| 503 |
if (uriString == null) { |
| 504 |
throw new NullPointerException("uriString"); |
| 505 |
} |
| 506 |
|
| 507 |
this.uriString = uriString; |
| 508 |
} |
| 509 |
|
| 510 |
static Uri readFrom(Parcel parcel) { |
| 511 |
return new StringUri(parcel.readString8()); |
| 512 |
} |
| 513 |
|
| 514 |
public int describeContents() { |
| 515 |
return 0; |
| 516 |
} |
| 517 |
|
| 518 |
public void writeToParcel(Parcel parcel, int flags) { |
| 519 |
parcel.writeInt(TYPE_ID); |
| 520 |
parcel.writeString8(uriString); |
| 521 |
} |
| 522 |
|
| 523 |
|
| 524 |
private volatile int cachedSsi = NOT_CALCULATED; |
| 525 |
|
| 526 |
|
| 527 |
private int findSchemeSeparator() { |
| 528 |
return cachedSsi == NOT_CALCULATED |
| 529 |
? cachedSsi = uriString.indexOf(':') |
| 530 |
: cachedSsi; |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
private volatile int cachedFsi = NOT_CALCULATED; |
| 535 |
|
| 536 |
|
| 537 |
private int findFragmentSeparator() { |
| 538 |
return cachedFsi == NOT_CALCULATED |
| 539 |
? cachedFsi = uriString.indexOf('#', findSchemeSeparator()) |
| 540 |
: cachedFsi; |
| 541 |
} |
| 542 |
|
| 543 |
public boolean isHierarchical() { |
| 544 |
int ssi = findSchemeSeparator(); |
| 545 |
|
| 546 |
if (ssi == NOT_FOUND) { |
| 547 |
|
| 548 |
return true; |
| 549 |
} |
| 550 |
|
| 551 |
if (uriString.length() == ssi + 1) { |
| 552 |
|
| 553 |
return false; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
return uriString.charAt(ssi + 1) == '/'; |
| 558 |
} |
| 559 |
|
| 560 |
public boolean isRelative() { |
| 561 |
|
| 562 |
return findSchemeSeparator() == NOT_FOUND; |
| 563 |
} |
| 564 |
|
| 565 |
private volatile String scheme = NotCachedHolder.NOT_CACHED; |
| 566 |
|
| 567 |
public String getScheme() { |
| 568 |
@SuppressWarnings("StringEquality") |
| 569 |
boolean cached = (scheme != NotCachedHolder.NOT_CACHED); |
| 570 |
return cached ? scheme : (scheme = parseScheme()); |
| 571 |
} |
| 572 |
|
| 573 |
private String parseScheme() { |
| 574 |
int ssi = findSchemeSeparator(); |
| 575 |
return ssi == NOT_FOUND ? null : uriString.substring(0, ssi); |
| 576 |
} |
| 577 |
|
| 578 |
private Part ssp; |
| 579 |
|
| 580 |
private Part getSsp() { |
| 581 |
return ssp == null ? ssp = Part.fromEncoded(parseSsp()) : ssp; |
| 582 |
} |
| 583 |
|
| 584 |
public String getEncodedSchemeSpecificPart() { |
| 585 |
return getSsp().getEncoded(); |
| 586 |
} |
| 587 |
|
| 588 |
public String getSchemeSpecificPart() { |
| 589 |
return getSsp().getDecoded(); |
| 590 |
} |
| 591 |
|
| 592 |
private String parseSsp() { |
| 593 |
int ssi = findSchemeSeparator(); |
| 594 |
int fsi = findFragmentSeparator(); |
| 595 |
|
| 596 |
|
| 597 |
return fsi == NOT_FOUND |
| 598 |
? uriString.substring(ssi + 1) |
| 599 |
: uriString.substring(ssi + 1, fsi); |
| 600 |
} |
| 601 |
|
| 602 |
private Part authority; |
| 603 |
|
| 604 |
private Part getAuthorityPart() { |
| 605 |
if (authority == null) { |
| 606 |
String encodedAuthority |
| 607 |
= parseAuthority(this.uriString, findSchemeSeparator()); |
| 608 |
return authority = Part.fromEncoded(encodedAuthority); |
| 609 |
} |
| 610 |
|
| 611 |
return authority; |
| 612 |
} |
| 613 |
|
| 614 |
public String getEncodedAuthority() { |
| 615 |
return getAuthorityPart().getEncoded(); |
| 616 |
} |
| 617 |
|
| 618 |
public String getAuthority() { |
| 619 |
return getAuthorityPart().getDecoded(); |
| 620 |
} |
| 621 |
|
| 622 |
private PathPart path; |
| 623 |
|
| 624 |
private PathPart getPathPart() { |
| 625 |
return path == null |
| 626 |
? path = PathPart.fromEncoded(parsePath()) |
| 627 |
: path; |
| 628 |
} |
| 629 |
|
| 630 |
public String getPath() { |
| 631 |
return getPathPart().getDecoded(); |
| 632 |
} |
| 633 |
|
| 634 |
public String getEncodedPath() { |
| 635 |
return getPathPart().getEncoded(); |
| 636 |
} |
| 637 |
|
| 638 |
public List<String> getPathSegments() { |
| 639 |
return getPathPart().getPathSegments(); |
| 640 |
} |
| 641 |
|
| 642 |
private String parsePath() { |
| 643 |
String uriString = this.uriString; |
| 644 |
int ssi = findSchemeSeparator(); |
| 645 |
|
| 646 |
|
| 647 |
if (ssi > -1) { |
| 648 |
|
| 649 |
boolean schemeOnly = ssi + 1 == uriString.length(); |
| 650 |
if (schemeOnly) { |
| 651 |
|
| 652 |
return null; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
if (uriString.charAt(ssi + 1) != '/') { |
| 657 |
|
| 658 |
return null; |
| 659 |
} |
| 660 |
} else { |
| 661 |
|
| 662 |
} |
| 663 |
|
| 664 |
return parsePath(uriString, ssi); |
| 665 |
} |
| 666 |
|
| 667 |
private Part query; |
| 668 |
|
| 669 |
private Part getQueryPart() { |
| 670 |
return query == null |
| 671 |
? query = Part.fromEncoded(parseQuery()) : query; |
| 672 |
} |
| 673 |
|
| 674 |
public String getEncodedQuery() { |
| 675 |
return getQueryPart().getEncoded(); |
| 676 |
} |
| 677 |
|
| 678 |
private String parseQuery() { |
| 679 |
|
| 680 |
|
| 681 |
int qsi = uriString.indexOf('?', findSchemeSeparator()); |
| 682 |
if (qsi == NOT_FOUND) { |
| 683 |
return null; |
| 684 |
} |
| 685 |
|
| 686 |
int fsi = findFragmentSeparator(); |
| 687 |
|
| 688 |
if (fsi == NOT_FOUND) { |
| 689 |
return uriString.substring(qsi + 1); |
| 690 |
} |
| 691 |
|
| 692 |
if (fsi < qsi) { |
| 693 |
|
| 694 |
return null; |
| 695 |
} |
| 696 |
|
| 697 |
return uriString.substring(qsi + 1, fsi); |
| 698 |
} |
| 699 |
|
| 700 |
public String getQuery() { |
| 701 |
return getQueryPart().getDecoded(); |
| 702 |
} |
| 703 |
|
| 704 |
private Part fragment; |
| 705 |
|
| 706 |
private Part getFragmentPart() { |
| 707 |
return fragment == null |
| 708 |
? fragment = Part.fromEncoded(parseFragment()) : fragment; |
| 709 |
} |
| 710 |
|
| 711 |
public String getEncodedFragment() { |
| 712 |
return getFragmentPart().getEncoded(); |
| 713 |
} |
| 714 |
|
| 715 |
private String parseFragment() { |
| 716 |
int fsi = findFragmentSeparator(); |
| 717 |
return fsi == NOT_FOUND ? null : uriString.substring(fsi + 1); |
| 718 |
} |
| 719 |
|
| 720 |
public String getFragment() { |
| 721 |
return getFragmentPart().getDecoded(); |
| 722 |
} |
| 723 |
|
| 724 |
public String toString() { |
| 725 |
return uriString; |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
static String parseAuthority(String uriString, int ssi) { |
| 737 |
int length = uriString.length(); |
| 738 |
|
| 739 |
|
| 740 |
if (length > ssi + 2 |
| 741 |
&& uriString.charAt(ssi + 1) == '/' |
| 742 |
&& uriString.charAt(ssi + 2) == '/') { |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
int end = ssi + 3; |
| 748 |
LOOP: while (end < length) { |
| 749 |
switch (uriString.charAt(end)) { |
| 750 |
case '/': |
| 751 |
case '\\': |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
case '?': |
| 756 |
case '#': |
| 757 |
break LOOP; |
| 758 |
} |
| 759 |
end++; |
| 760 |
} |
| 761 |
|
| 762 |
return uriString.substring(ssi + 3, end); |
| 763 |
} else { |
| 764 |
return null; |
| 765 |
} |
| 766 |
|
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
static String parsePath(String uriString, int ssi) { |
| 778 |
int length = uriString.length(); |
| 779 |
|
| 780 |
|
| 781 |
int pathStart; |
| 782 |
if (length > ssi + 2 |
| 783 |
&& uriString.charAt(ssi + 1) == '/' |
| 784 |
&& uriString.charAt(ssi + 2) == '/') { |
| 785 |
|
| 786 |
pathStart = ssi + 3; |
| 787 |
LOOP: while (pathStart < length) { |
| 788 |
switch (uriString.charAt(pathStart)) { |
| 789 |
case '?': |
| 790 |
case '#': |
| 791 |
return ""; |
| 792 |
case '/': |
| 793 |
case '\\': |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
break LOOP; |
| 798 |
} |
| 799 |
pathStart++; |
| 800 |
} |
| 801 |
} else { |
| 802 |
|
| 803 |
pathStart = ssi + 1; |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
int pathEnd = pathStart; |
| 808 |
LOOP: while (pathEnd < length) { |
| 809 |
switch (uriString.charAt(pathEnd)) { |
| 810 |
case '?': |
| 811 |
case '#': |
| 812 |
break LOOP; |
| 813 |
} |
| 814 |
pathEnd++; |
| 815 |
} |
| 816 |
|
| 817 |
return uriString.substring(pathStart, pathEnd); |
| 818 |
} |
| 819 |
|
| 820 |
public Builder buildUpon() { |
| 821 |
if (isHierarchical()) { |
| 822 |
return new Builder() |
| 823 |
.scheme(getScheme()) |
| 824 |
.authority(getAuthorityPart()) |
| 825 |
.path(getPathPart()) |
| 826 |
.query(getQueryPart()) |
| 827 |
.fragment(getFragmentPart()); |
| 828 |
} else { |
| 829 |
return new Builder() |
| 830 |
.scheme(getScheme()) |
| 831 |
.opaquePart(getSsp()) |
| 832 |
.fragment(getFragmentPart()); |
| 833 |
} |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
public static Uri fromParts(String scheme, String ssp, |
| 854 |
String fragment) { |
| 855 |
if (scheme == null) { |
| 856 |
throw new NullPointerException("scheme"); |
| 857 |
} |
| 858 |
if (ssp == null) { |
| 859 |
throw new NullPointerException("ssp"); |
| 860 |
} |
| 861 |
|
| 862 |
return new OpaqueUri(scheme, Part.fromDecoded(ssp), |
| 863 |
Part.fromDecoded(fragment)); |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
private static class OpaqueUri extends Uri { |
| 870 |
|
| 871 |
|
| 872 |
static final int TYPE_ID = 2; |
| 873 |
|
| 874 |
private final String scheme; |
| 875 |
private final Part ssp; |
| 876 |
private final Part fragment; |
| 877 |
|
| 878 |
private OpaqueUri(String scheme, Part ssp, Part fragment) { |
| 879 |
this.scheme = scheme; |
| 880 |
this.ssp = ssp; |
| 881 |
this.fragment = fragment == null ? Part.NULL : fragment; |
| 882 |
} |
| 883 |
|
| 884 |
static Uri readFrom(Parcel parcel) { |
| 885 |
return new OpaqueUri( |
| 886 |
parcel.readString8(), |
| 887 |
Part.readFrom(parcel), |
| 888 |
Part.readFrom(parcel) |
| 889 |
); |
| 890 |
} |
| 891 |
|
| 892 |
public int describeContents() { |
| 893 |
return 0; |
| 894 |
} |
| 895 |
|
| 896 |
public void writeToParcel(Parcel parcel, int flags) { |
| 897 |
parcel.writeInt(TYPE_ID); |
| 898 |
parcel.writeString8(scheme); |
| 899 |
ssp.writeTo(parcel); |
| 900 |
fragment.writeTo(parcel); |
| 901 |
} |
| 902 |
|
| 903 |
public boolean isHierarchical() { |
| 904 |
return false; |
| 905 |
} |
| 906 |
|
| 907 |
public boolean isRelative() { |
| 908 |
return scheme == null; |
| 909 |
} |
| 910 |
|
| 911 |
public String getScheme() { |
| 912 |
return this.scheme; |
| 913 |
} |
| 914 |
|
| 915 |
public String getEncodedSchemeSpecificPart() { |
| 916 |
return ssp.getEncoded(); |
| 917 |
} |
| 918 |
|
| 919 |
public String getSchemeSpecificPart() { |
| 920 |
return ssp.getDecoded(); |
| 921 |
} |
| 922 |
|
| 923 |
public String getAuthority() { |
| 924 |
return null; |
| 925 |
} |
| 926 |
|
| 927 |
public String getEncodedAuthority() { |
| 928 |
return null; |
| 929 |
} |
| 930 |
|
| 931 |
public String getPath() { |
| 932 |
return null; |
| 933 |
} |
| 934 |
|
| 935 |
public String getEncodedPath() { |
| 936 |
return null; |
| 937 |
} |
| 938 |
|
| 939 |
public String getQuery() { |
| 940 |
return null; |
| 941 |
} |
| 942 |
|
| 943 |
public String getEncodedQuery() { |
| 944 |
return null; |
| 945 |
} |
| 946 |
|
| 947 |
public String getFragment() { |
| 948 |
return fragment.getDecoded(); |
| 949 |
} |
| 950 |
|
| 951 |
public String getEncodedFragment() { |
| 952 |
return fragment.getEncoded(); |
| 953 |
} |
| 954 |
|
| 955 |
public List<String> getPathSegments() { |
| 956 |
return Collections.emptyList(); |
| 957 |
} |
| 958 |
|
| 959 |
public String getLastPathSegment() { |
| 960 |
return null; |
| 961 |
} |
| 962 |
|
| 963 |
public String getUserInfo() { |
| 964 |
return null; |
| 965 |
} |
| 966 |
|
| 967 |
public String getEncodedUserInfo() { |
| 968 |
return null; |
| 969 |
} |
| 970 |
|
| 971 |
public String getHost() { |
| 972 |
return null; |
| 973 |
} |
| 974 |
|
| 975 |
public int getPort() { |
| 976 |
return -1; |
| 977 |
} |
| 978 |
|
| 979 |
private volatile String cachedString = NotCachedHolder.NOT_CACHED; |
| 980 |
|
| 981 |
public String toString() { |
| 982 |
@SuppressWarnings("StringEquality") |
| 983 |
boolean cached = cachedString != NotCachedHolder.NOT_CACHED; |
| 984 |
if (cached) { |
| 985 |
return cachedString; |
| 986 |
} |
| 987 |
|
| 988 |
StringBuilder sb = new StringBuilder(); |
| 989 |
|
| 990 |
sb.append(scheme).append(':'); |
| 991 |
sb.append(getEncodedSchemeSpecificPart()); |
| 992 |
|
| 993 |
if (!fragment.isEmpty()) { |
| 994 |
sb.append('#').append(fragment.getEncoded()); |
| 995 |
} |
| 996 |
|
| 997 |
return cachedString = sb.toString(); |
| 998 |
} |
| 999 |
|
| 1000 |
public Builder buildUpon() { |
| 1001 |
return new Builder() |
| 1002 |
.scheme(this.scheme) |
| 1003 |
.opaquePart(this.ssp) |
| 1004 |
.fragment(this.fragment); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
static class PathSegments extends AbstractList<String> |
| 1012 |
implements RandomAccess { |
| 1013 |
|
| 1014 |
static final PathSegments EMPTY = new PathSegments(null, 0); |
| 1015 |
|
| 1016 |
final String[] segments; |
| 1017 |
final int size; |
| 1018 |
|
| 1019 |
PathSegments(String[] segments, int size) { |
| 1020 |
this.segments = segments; |
| 1021 |
this.size = size; |
| 1022 |
} |
| 1023 |
|
| 1024 |
public String get(int index) { |
| 1025 |
if (index >= size) { |
| 1026 |
throw new IndexOutOfBoundsException(); |
| 1027 |
} |
| 1028 |
|
| 1029 |
return segments[index]; |
| 1030 |
} |
| 1031 |
|
| 1032 |
public int size() { |
| 1033 |
return this.size; |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
static class PathSegmentsBuilder { |
| 1041 |
|
| 1042 |
String[] segments; |
| 1043 |
int size = 0; |
| 1044 |
|
| 1045 |
void add(String segment) { |
| 1046 |
if (segments == null) { |
| 1047 |
segments = new String[4]; |
| 1048 |
} else if (size + 1 == segments.length) { |
| 1049 |
String[] expanded = new String[segments.length * 2]; |
| 1050 |
System.arraycopy(segments, 0, expanded, 0, segments.length); |
| 1051 |
segments = expanded; |
| 1052 |
} |
| 1053 |
|
| 1054 |
segments[size++] = segment; |
| 1055 |
} |
| 1056 |
|
| 1057 |
PathSegments build() { |
| 1058 |
if (segments == null) { |
| 1059 |
return PathSegments.EMPTY; |
| 1060 |
} |
| 1061 |
|
| 1062 |
try { |
| 1063 |
return new PathSegments(segments, size); |
| 1064 |
} finally { |
| 1065 |
|
| 1066 |
segments = null; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
private abstract static class AbstractHierarchicalUri extends Uri { |
| 1075 |
|
| 1076 |
public String getLastPathSegment() { |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
List<String> segments = getPathSegments(); |
| 1081 |
int size = segments.size(); |
| 1082 |
if (size == 0) { |
| 1083 |
return null; |
| 1084 |
} |
| 1085 |
return segments.get(size - 1); |
| 1086 |
} |
| 1087 |
|
| 1088 |
private Part userInfo; |
| 1089 |
|
| 1090 |
private Part getUserInfoPart() { |
| 1091 |
return userInfo == null |
| 1092 |
? userInfo = Part.fromEncoded(parseUserInfo()) : userInfo; |
| 1093 |
} |
| 1094 |
|
| 1095 |
public final String getEncodedUserInfo() { |
| 1096 |
return getUserInfoPart().getEncoded(); |
| 1097 |
} |
| 1098 |
|
| 1099 |
private String parseUserInfo() { |
| 1100 |
String authority = getEncodedAuthority(); |
| 1101 |
if (authority == null) { |
| 1102 |
return null; |
| 1103 |
} |
| 1104 |
|
| 1105 |
int end = authority.lastIndexOf('@'); |
| 1106 |
return end == NOT_FOUND ? null : authority.substring(0, end); |
| 1107 |
} |
| 1108 |
|
| 1109 |
public String getUserInfo() { |
| 1110 |
return getUserInfoPart().getDecoded(); |
| 1111 |
} |
| 1112 |
|
| 1113 |
private volatile String host = NotCachedHolder.NOT_CACHED; |
| 1114 |
|
| 1115 |
public String getHost() { |
| 1116 |
@SuppressWarnings("StringEquality") |
| 1117 |
boolean cached = (host != NotCachedHolder.NOT_CACHED); |
| 1118 |
return cached ? host : (host = parseHost()); |
| 1119 |
} |
| 1120 |
|
| 1121 |
private String parseHost() { |
| 1122 |
final String authority = getEncodedAuthority(); |
| 1123 |
if (authority == null) { |
| 1124 |
return null; |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
int userInfoSeparator = authority.lastIndexOf('@'); |
| 1129 |
int portSeparator = findPortSeparator(authority); |
| 1130 |
|
| 1131 |
String encodedHost = portSeparator == NOT_FOUND |
| 1132 |
? authority.substring(userInfoSeparator + 1) |
| 1133 |
: authority.substring(userInfoSeparator + 1, portSeparator); |
| 1134 |
|
| 1135 |
return decode(encodedHost); |
| 1136 |
} |
| 1137 |
|
| 1138 |
private volatile int port = NOT_CALCULATED; |
| 1139 |
|
| 1140 |
public int getPort() { |
| 1141 |
return port == NOT_CALCULATED |
| 1142 |
? port = parsePort() |
| 1143 |
: port; |
| 1144 |
} |
| 1145 |
|
| 1146 |
private int parsePort() { |
| 1147 |
final String authority = getEncodedAuthority(); |
| 1148 |
int portSeparator = findPortSeparator(authority); |
| 1149 |
if (portSeparator == NOT_FOUND) { |
| 1150 |
return -1; |
| 1151 |
} |
| 1152 |
|
| 1153 |
String portString = decode(authority.substring(portSeparator + 1)); |
| 1154 |
try { |
| 1155 |
return Integer.parseInt(portString); |
| 1156 |
} catch (NumberFormatException e) { |
| 1157 |
Log.w(LOG, "Error parsing port string.", e); |
| 1158 |
return -1; |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
private int findPortSeparator(String authority) { |
| 1163 |
if (authority == null) { |
| 1164 |
return NOT_FOUND; |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
for (int i = authority.length() - 1; i >= 0; --i) { |
| 1172 |
final int character = authority.charAt(i); |
| 1173 |
if (':' == character) return i; |
| 1174 |
|
| 1175 |
if (character < '0' || character > '9') return NOT_FOUND; |
| 1176 |
} |
| 1177 |
return NOT_FOUND; |
| 1178 |
} |
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
private static class HierarchicalUri extends AbstractHierarchicalUri { |
| 1185 |
|
| 1186 |
|
| 1187 |
static final int TYPE_ID = 3; |
| 1188 |
|
| 1189 |
private final String scheme; |
| 1190 |
private final Part authority; |
| 1191 |
private final PathPart path; |
| 1192 |
private final Part query; |
| 1193 |
private final Part fragment; |
| 1194 |
|
| 1195 |
private HierarchicalUri(String scheme, Part authority, PathPart path, |
| 1196 |
Part query, Part fragment) { |
| 1197 |
this.scheme = scheme; |
| 1198 |
this.authority = Part.nonNull(authority); |
| 1199 |
this.path = path == null ? PathPart.NULL : path; |
| 1200 |
this.query = Part.nonNull(query); |
| 1201 |
this.fragment = Part.nonNull(fragment); |
| 1202 |
} |
| 1203 |
|
| 1204 |
static Uri readFrom(Parcel parcel) { |
| 1205 |
final String scheme = parcel.readString8(); |
| 1206 |
final Part authority = Part.readFrom(parcel); |
| 1207 |
|
| 1208 |
|
| 1209 |
final boolean hasSchemeOrAuthority = |
| 1210 |
(scheme != null && scheme.length() > 0) || !authority.isEmpty(); |
| 1211 |
final PathPart path = PathPart.readFrom(hasSchemeOrAuthority, parcel); |
| 1212 |
final Part query = Part.readFrom(parcel); |
| 1213 |
final Part fragment = Part.readFrom(parcel); |
| 1214 |
return new HierarchicalUri(scheme, authority, path, query, fragment); |
| 1215 |
} |
| 1216 |
|
| 1217 |
public int describeContents() { |
| 1218 |
return 0; |
| 1219 |
} |
| 1220 |
|
| 1221 |
public void writeToParcel(Parcel parcel, int flags) { |
| 1222 |
parcel.writeInt(TYPE_ID); |
| 1223 |
parcel.writeString8(scheme); |
| 1224 |
authority.writeTo(parcel); |
| 1225 |
path.writeTo(parcel); |
| 1226 |
query.writeTo(parcel); |
| 1227 |
fragment.writeTo(parcel); |
| 1228 |
} |
| 1229 |
|
| 1230 |
public boolean isHierarchical() { |
| 1231 |
return true; |
| 1232 |
} |
| 1233 |
|
| 1234 |
public boolean isRelative() { |
| 1235 |
return scheme == null; |
| 1236 |
} |
| 1237 |
|
| 1238 |
public String getScheme() { |
| 1239 |
return scheme; |
| 1240 |
} |
| 1241 |
|
| 1242 |
private Part ssp; |
| 1243 |
|
| 1244 |
private Part getSsp() { |
| 1245 |
return ssp == null |
| 1246 |
? ssp = Part.fromEncoded(makeSchemeSpecificPart()) : ssp; |
| 1247 |
} |
| 1248 |
|
| 1249 |
public String getEncodedSchemeSpecificPart() { |
| 1250 |
return getSsp().getEncoded(); |
| 1251 |
} |
| 1252 |
|
| 1253 |
public String getSchemeSpecificPart() { |
| 1254 |
return getSsp().getDecoded(); |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
private String makeSchemeSpecificPart() { |
| 1261 |
StringBuilder builder = new StringBuilder(); |
| 1262 |
appendSspTo(builder); |
| 1263 |
return builder.toString(); |
| 1264 |
} |
| 1265 |
|
| 1266 |
private void appendSspTo(StringBuilder builder) { |
| 1267 |
String encodedAuthority = authority.getEncoded(); |
| 1268 |
if (encodedAuthority != null) { |
| 1269 |
|
| 1270 |
builder.append("//").append(encodedAuthority); |
| 1271 |
} |
| 1272 |
|
| 1273 |
String encodedPath = path.getEncoded(); |
| 1274 |
if (encodedPath != null) { |
| 1275 |
builder.append(encodedPath); |
| 1276 |
} |
| 1277 |
|
| 1278 |
if (!query.isEmpty()) { |
| 1279 |
builder.append('?').append(query.getEncoded()); |
| 1280 |
} |
| 1281 |
} |
| 1282 |
|
| 1283 |
public String getAuthority() { |
| 1284 |
return this.authority.getDecoded(); |
| 1285 |
} |
| 1286 |
|
| 1287 |
public String getEncodedAuthority() { |
| 1288 |
return this.authority.getEncoded(); |
| 1289 |
} |
| 1290 |
|
| 1291 |
public String getEncodedPath() { |
| 1292 |
return this.path.getEncoded(); |
| 1293 |
} |
| 1294 |
|
| 1295 |
public String getPath() { |
| 1296 |
return this.path.getDecoded(); |
| 1297 |
} |
| 1298 |
|
| 1299 |
public String getQuery() { |
| 1300 |
return this.query.getDecoded(); |
| 1301 |
} |
| 1302 |
|
| 1303 |
public String getEncodedQuery() { |
| 1304 |
return this.query.getEncoded(); |
| 1305 |
} |
| 1306 |
|
| 1307 |
public String getFragment() { |
| 1308 |
return this.fragment.getDecoded(); |
| 1309 |
} |
| 1310 |
|
| 1311 |
public String getEncodedFragment() { |
| 1312 |
return this.fragment.getEncoded(); |
| 1313 |
} |
| 1314 |
|
| 1315 |
public List<String> getPathSegments() { |
| 1316 |
return this.path.getPathSegments(); |
| 1317 |
} |
| 1318 |
|
| 1319 |
private volatile String uriString = NotCachedHolder.NOT_CACHED; |
| 1320 |
|
| 1321 |
@Override |
| 1322 |
public String toString() { |
| 1323 |
@SuppressWarnings("StringEquality") |
| 1324 |
boolean cached = (uriString != NotCachedHolder.NOT_CACHED); |
| 1325 |
return cached ? uriString |
| 1326 |
: (uriString = makeUriString()); |
| 1327 |
} |
| 1328 |
|
| 1329 |
private String makeUriString() { |
| 1330 |
StringBuilder builder = new StringBuilder(); |
| 1331 |
|
| 1332 |
if (scheme != null) { |
| 1333 |
builder.append(scheme).append(':'); |
| 1334 |
} |
| 1335 |
|
| 1336 |
appendSspTo(builder); |
| 1337 |
|
| 1338 |
if (!fragment.isEmpty()) { |
| 1339 |
builder.append('#').append(fragment.getEncoded()); |
| 1340 |
} |
| 1341 |
|
| 1342 |
return builder.toString(); |
| 1343 |
} |
| 1344 |
|
| 1345 |
public Builder buildUpon() { |
| 1346 |
return new Builder() |
| 1347 |
.scheme(scheme) |
| 1348 |
.authority(authority) |
| 1349 |
.path(path) |
| 1350 |
.query(query) |
| 1351 |
.fragment(fragment); |
| 1352 |
} |
| 1353 |
} |
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
public static final class Builder { |
| 1372 |
|
| 1373 |
private String scheme; |
| 1374 |
private Part opaquePart; |
| 1375 |
private Part authority; |
| 1376 |
private PathPart path; |
| 1377 |
private Part query; |
| 1378 |
private Part fragment; |
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
public Builder() {} |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
public Builder scheme(String scheme) { |
| 1391 |
this.scheme = scheme; |
| 1392 |
return this; |
| 1393 |
} |
| 1394 |
|
| 1395 |
Builder opaquePart(Part opaquePart) { |
| 1396 |
this.opaquePart = opaquePart; |
| 1397 |
return this; |
| 1398 |
} |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
public Builder opaquePart(String opaquePart) { |
| 1406 |
return opaquePart(Part.fromDecoded(opaquePart)); |
| 1407 |
} |
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
public Builder encodedOpaquePart(String opaquePart) { |
| 1415 |
return opaquePart(Part.fromEncoded(opaquePart)); |
| 1416 |
} |
| 1417 |
|
| 1418 |
Builder authority(Part authority) { |
| 1419 |
|
| 1420 |
this.opaquePart = null; |
| 1421 |
|
| 1422 |
this.authority = authority; |
| 1423 |
return this; |
| 1424 |
} |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
public Builder authority(String authority) { |
| 1430 |
return authority(Part.fromDecoded(authority)); |
| 1431 |
} |
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
public Builder encodedAuthority(String authority) { |
| 1437 |
return authority(Part.fromEncoded(authority)); |
| 1438 |
} |
| 1439 |
|
| 1440 |
Builder path(PathPart path) { |
| 1441 |
|
| 1442 |
this.opaquePart = null; |
| 1443 |
|
| 1444 |
this.path = path; |
| 1445 |
return this; |
| 1446 |
} |
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
public Builder path(String path) { |
| 1457 |
return path(PathPart.fromDecoded(path)); |
| 1458 |
} |
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
public Builder encodedPath(String path) { |
| 1468 |
return path(PathPart.fromEncoded(path)); |
| 1469 |
} |
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
public Builder appendPath(String newSegment) { |
| 1475 |
return path(PathPart.appendDecodedSegment(path, newSegment)); |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
public Builder appendEncodedPath(String newSegment) { |
| 1482 |
return path(PathPart.appendEncodedSegment(path, newSegment)); |
| 1483 |
} |
| 1484 |
|
| 1485 |
Builder query(Part query) { |
| 1486 |
|
| 1487 |
this.opaquePart = null; |
| 1488 |
|
| 1489 |
this.query = query; |
| 1490 |
return this; |
| 1491 |
} |
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
public Builder query(String query) { |
| 1497 |
return query(Part.fromDecoded(query)); |
| 1498 |
} |
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
public Builder encodedQuery(String query) { |
| 1504 |
return query(Part.fromEncoded(query)); |
| 1505 |
} |
| 1506 |
|
| 1507 |
Builder fragment(Part fragment) { |
| 1508 |
this.fragment = fragment; |
| 1509 |
return this; |
| 1510 |
} |
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
public Builder fragment(String fragment) { |
| 1516 |
return fragment(Part.fromDecoded(fragment)); |
| 1517 |
} |
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
public Builder encodedFragment(String fragment) { |
| 1523 |
return fragment(Part.fromEncoded(fragment)); |
| 1524 |
} |
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
public Builder appendQueryParameter(String key, String value) { |
| 1534 |
|
| 1535 |
this.opaquePart = null; |
| 1536 |
|
| 1537 |
String encodedParameter = encode(key, null) + "=" |
| 1538 |
+ encode(value, null); |
| 1539 |
|
| 1540 |
if (query == null) { |
| 1541 |
query = Part.fromEncoded(encodedParameter); |
| 1542 |
return this; |
| 1543 |
} |
| 1544 |
|
| 1545 |
String oldQuery = query.getEncoded(); |
| 1546 |
if (oldQuery == null || oldQuery.length() == 0) { |
| 1547 |
query = Part.fromEncoded(encodedParameter); |
| 1548 |
} else { |
| 1549 |
query = Part.fromEncoded(oldQuery + "&" + encodedParameter); |
| 1550 |
} |
| 1551 |
|
| 1552 |
return this; |
| 1553 |
} |
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
public Builder clearQuery() { |
| 1559 |
return query((Part) null); |
| 1560 |
} |
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
public Uri build() { |
| 1569 |
if (opaquePart != null) { |
| 1570 |
if (this.scheme == null) { |
| 1571 |
throw new UnsupportedOperationException( |
| 1572 |
"An opaque URI must have a scheme."); |
| 1573 |
} |
| 1574 |
|
| 1575 |
return new OpaqueUri(scheme, opaquePart, fragment); |
| 1576 |
} else { |
| 1577 |
|
| 1578 |
PathPart path = this.path; |
| 1579 |
if (path == null || path == PathPart.NULL) { |
| 1580 |
path = PathPart.EMPTY; |
| 1581 |
} else { |
| 1582 |
|
| 1583 |
|
| 1584 |
if (hasSchemeOrAuthority()) { |
| 1585 |
path = PathPart.makeAbsolute(path); |
| 1586 |
} |
| 1587 |
} |
| 1588 |
|
| 1589 |
return new HierarchicalUri( |
| 1590 |
scheme, authority, path, query, fragment); |
| 1591 |
} |
| 1592 |
} |
| 1593 |
|
| 1594 |
private boolean hasSchemeOrAuthority() { |
| 1595 |
return scheme != null |
| 1596 |
|| (authority != null && authority != Part.NULL); |
| 1597 |
|
| 1598 |
} |
| 1599 |
|
| 1600 |
@Override |
| 1601 |
public String toString() { |
| 1602 |
return build().toString(); |
| 1603 |
} |
| 1604 |
} |
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
public Set<String> getQueryParameterNames() { |
| 1615 |
if (isOpaque()) { |
| 1616 |
throw new UnsupportedOperationException(NOT_HIERARCHICAL); |
| 1617 |
} |
| 1618 |
|
| 1619 |
String query = getEncodedQuery(); |
| 1620 |
if (query == null) { |
| 1621 |
return Collections.emptySet(); |
| 1622 |
} |
| 1623 |
|
| 1624 |
Set<String> names = new LinkedHashSet<String>(); |
| 1625 |
int start = 0; |
| 1626 |
do { |
| 1627 |
int next = query.indexOf('&', start); |
| 1628 |
int end = (next == -1) ? query.length() : next; |
| 1629 |
|
| 1630 |
int separator = query.indexOf('=', start); |
| 1631 |
if (separator > end || separator == -1) { |
| 1632 |
separator = end; |
| 1633 |
} |
| 1634 |
|
| 1635 |
String name = query.substring(start, separator); |
| 1636 |
names.add(decode(name)); |
| 1637 |
|
| 1638 |
|
| 1639 |
start = end + 1; |
| 1640 |
} while (start < query.length()); |
| 1641 |
|
| 1642 |
return Collections.unmodifiableSet(names); |
| 1643 |
} |
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
public List<String> getQueryParameters(String key) { |
| 1655 |
if (isOpaque()) { |
| 1656 |
throw new UnsupportedOperationException(NOT_HIERARCHICAL); |
| 1657 |
} |
| 1658 |
if (key == null) { |
| 1659 |
throw new NullPointerException("key"); |
| 1660 |
} |
| 1661 |
|
| 1662 |
String query = getEncodedQuery(); |
| 1663 |
if (query == null) { |
| 1664 |
return Collections.emptyList(); |
| 1665 |
} |
| 1666 |
|
| 1667 |
String encodedKey; |
| 1668 |
try { |
| 1669 |
encodedKey = URLEncoder.encode(key, DEFAULT_ENCODING); |
| 1670 |
} catch (UnsupportedEncodingException e) { |
| 1671 |
throw new AssertionError(e); |
| 1672 |
} |
| 1673 |
|
| 1674 |
ArrayList<String> values = new ArrayList<String>(); |
| 1675 |
|
| 1676 |
int start = 0; |
| 1677 |
do { |
| 1678 |
int nextAmpersand = query.indexOf('&', start); |
| 1679 |
int end = nextAmpersand != -1 ? nextAmpersand : query.length(); |
| 1680 |
|
| 1681 |
int separator = query.indexOf('=', start); |
| 1682 |
if (separator > end || separator == -1) { |
| 1683 |
separator = end; |
| 1684 |
} |
| 1685 |
|
| 1686 |
if (separator - start == encodedKey.length() |
| 1687 |
&& query.regionMatches(start, encodedKey, 0, encodedKey.length())) { |
| 1688 |
if (separator == end) { |
| 1689 |
values.add(""); |
| 1690 |
} else { |
| 1691 |
values.add(decode(query.substring(separator + 1, end))); |
| 1692 |
} |
| 1693 |
} |
| 1694 |
|
| 1695 |
|
| 1696 |
if (nextAmpersand != -1) { |
| 1697 |
start = nextAmpersand + 1; |
| 1698 |
} else { |
| 1699 |
break; |
| 1700 |
} |
| 1701 |
} while (true); |
| 1702 |
|
| 1703 |
return Collections.unmodifiableList(values); |
| 1704 |
} |
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
@Nullable |
| 1718 |
public String getQueryParameter(String key) { |
| 1719 |
if (isOpaque()) { |
| 1720 |
throw new UnsupportedOperationException(NOT_HIERARCHICAL); |
| 1721 |
} |
| 1722 |
if (key == null) { |
| 1723 |
throw new NullPointerException("key"); |
| 1724 |
} |
| 1725 |
|
| 1726 |
final String query = getEncodedQuery(); |
| 1727 |
if (query == null) { |
| 1728 |
return null; |
| 1729 |
} |
| 1730 |
|
| 1731 |
final String encodedKey = encode(key, null); |
| 1732 |
final int length = query.length(); |
| 1733 |
int start = 0; |
| 1734 |
do { |
| 1735 |
int nextAmpersand = query.indexOf('&', start); |
| 1736 |
int end = nextAmpersand != -1 ? nextAmpersand : length; |
| 1737 |
|
| 1738 |
int separator = query.indexOf('=', start); |
| 1739 |
if (separator > end || separator == -1) { |
| 1740 |
separator = end; |
| 1741 |
} |
| 1742 |
|
| 1743 |
if (separator - start == encodedKey.length() |
| 1744 |
&& query.regionMatches(start, encodedKey, 0, encodedKey.length())) { |
| 1745 |
if (separator == end) { |
| 1746 |
return ""; |
| 1747 |
} else { |
| 1748 |
String encodedValue = query.substring(separator + 1, end); |
| 1749 |
return UriCodec.decode(encodedValue, true, StandardCharsets.UTF_8, false); |
| 1750 |
} |
| 1751 |
} |
| 1752 |
|
| 1753 |
|
| 1754 |
if (nextAmpersand != -1) { |
| 1755 |
start = nextAmpersand + 1; |
| 1756 |
} else { |
| 1757 |
break; |
| 1758 |
} |
| 1759 |
} while (true); |
| 1760 |
return null; |
| 1761 |
} |
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
public boolean getBooleanQueryParameter(String key, boolean defaultValue) { |
| 1773 |
String flag = getQueryParameter(key); |
| 1774 |
if (flag == null) { |
| 1775 |
return defaultValue; |
| 1776 |
} |
| 1777 |
flag = flag.toLowerCase(Locale.ROOT); |
| 1778 |
return (!"false".equals(flag) && !"0".equals(flag)); |
| 1779 |
} |
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
public Uri normalizeScheme() { |
| 1803 |
String scheme = getScheme(); |
| 1804 |
if (scheme == null) return this; |
| 1805 |
String lowerScheme = scheme.toLowerCase(Locale.ROOT); |
| 1806 |
if (scheme.equals(lowerScheme)) return this; |
| 1807 |
|
| 1808 |
return buildUpon().scheme(lowerScheme).build(); |
| 1809 |
} |
| 1810 |
|
| 1811 |
|
| 1812 |
private static final int NULL_TYPE_ID = 0; |
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
public static final @android.annotation.NonNull Parcelable.Creator<Uri> CREATOR |
| 1818 |
= new Parcelable.Creator<Uri>() { |
| 1819 |
public Uri createFromParcel(Parcel in) { |
| 1820 |
int type = in.readInt(); |
| 1821 |
switch (type) { |
| 1822 |
case NULL_TYPE_ID: return null; |
| 1823 |
case StringUri.TYPE_ID: return StringUri.readFrom(in); |
| 1824 |
case OpaqueUri.TYPE_ID: return OpaqueUri.readFrom(in); |
| 1825 |
case HierarchicalUri.TYPE_ID: |
| 1826 |
return HierarchicalUri.readFrom(in); |
| 1827 |
} |
| 1828 |
|
| 1829 |
throw new IllegalArgumentException("Unknown URI type: " + type); |
| 1830 |
} |
| 1831 |
|
| 1832 |
public Uri[] newArray(int size) { |
| 1833 |
return new Uri[size]; |
| 1834 |
} |
| 1835 |
}; |
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
|
| 1841 |
|
| 1842 |
|
| 1843 |
public static void writeToParcel(Parcel out, Uri uri) { |
| 1844 |
if (uri == null) { |
| 1845 |
out.writeInt(NULL_TYPE_ID); |
| 1846 |
} else { |
| 1847 |
uri.writeToParcel(out, 0); |
| 1848 |
} |
| 1849 |
} |
| 1850 |
|
| 1851 |
private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray(); |
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
public static String encode(String s) { |
| 1864 |
return encode(s, null); |
| 1865 |
} |
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
public static String encode(String s, String allow) { |
| 1881 |
if (s == null) { |
| 1882 |
return null; |
| 1883 |
} |
| 1884 |
|
| 1885 |
|
| 1886 |
StringBuilder encoded = null; |
| 1887 |
|
| 1888 |
int oldLength = s.length(); |
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
int current = 0; |
| 1894 |
while (current < oldLength) { |
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
int nextToEncode = current; |
| 1899 |
while (nextToEncode < oldLength |
| 1900 |
&& isAllowed(s.charAt(nextToEncode), allow)) { |
| 1901 |
nextToEncode++; |
| 1902 |
} |
| 1903 |
|
| 1904 |
|
| 1905 |
if (nextToEncode == oldLength) { |
| 1906 |
if (current == 0) { |
| 1907 |
|
| 1908 |
return s; |
| 1909 |
} else { |
| 1910 |
|
| 1911 |
encoded.append(s, current, oldLength); |
| 1912 |
return encoded.toString(); |
| 1913 |
} |
| 1914 |
} |
| 1915 |
|
| 1916 |
if (encoded == null) { |
| 1917 |
encoded = new StringBuilder(); |
| 1918 |
} |
| 1919 |
|
| 1920 |
if (nextToEncode > current) { |
| 1921 |
|
| 1922 |
encoded.append(s, current, nextToEncode); |
| 1923 |
} else { |
| 1924 |
|
| 1925 |
} |
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
current = nextToEncode; |
| 1931 |
int nextAllowed = current + 1; |
| 1932 |
while (nextAllowed < oldLength |
| 1933 |
&& !isAllowed(s.charAt(nextAllowed), allow)) { |
| 1934 |
nextAllowed++; |
| 1935 |
} |
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
String toEncode = s.substring(current, nextAllowed); |
| 1940 |
try { |
| 1941 |
byte[] bytes = toEncode.getBytes(DEFAULT_ENCODING); |
| 1942 |
int bytesLength = bytes.length; |
| 1943 |
for (int i = 0; i < bytesLength; i++) { |
| 1944 |
encoded.append('%'); |
| 1945 |
encoded.append(HEX_DIGITS[(bytes[i] & 0xf0) >> 4]); |
| 1946 |
encoded.append(HEX_DIGITS[bytes[i] & 0xf]); |
| 1947 |
} |
| 1948 |
} catch (UnsupportedEncodingException e) { |
| 1949 |
throw new AssertionError(e); |
| 1950 |
} |
| 1951 |
|
| 1952 |
current = nextAllowed; |
| 1953 |
} |
| 1954 |
|
| 1955 |
|
| 1956 |
return encoded == null ? s : encoded.toString(); |
| 1957 |
} |
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
private static boolean isAllowed(char c, String allow) { |
| 1968 |
return (c >= 'A' && c <= 'Z') |
| 1969 |
|| (c >= 'a' && c <= 'z') |
| 1970 |
|| (c >= '0' && c <= '9') |
| 1971 |
|| "_-!.~'()*".indexOf(c) != NOT_FOUND |
| 1972 |
|| (allow != null && allow.indexOf(c) != NOT_FOUND); |
| 1973 |
} |
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
public static String decode(String s) { |
| 1985 |
if (s == null) { |
| 1986 |
return null; |
| 1987 |
} |
| 1988 |
return UriCodec.decode( |
| 1989 |
s, false , StandardCharsets.UTF_8, false ); |
| 1990 |
} |
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
static abstract class AbstractPart { |
| 1996 |
|
| 1997 |
|
| 1998 |
static final int REPRESENTATION_ENCODED = 1; |
| 1999 |
static final int REPRESENTATION_DECODED = 2; |
| 2000 |
|
| 2001 |
volatile String encoded; |
| 2002 |
volatile String decoded; |
| 2003 |
private final int mCanonicalRepresentation; |
| 2004 |
|
| 2005 |
AbstractPart(String encoded, String decoded) { |
| 2006 |
if (encoded != NotCachedHolder.NOT_CACHED) { |
| 2007 |
this.mCanonicalRepresentation = REPRESENTATION_ENCODED; |
| 2008 |
this.encoded = encoded; |
| 2009 |
this.decoded = NotCachedHolder.NOT_CACHED; |
| 2010 |
} else if (decoded != NotCachedHolder.NOT_CACHED) { |
| 2011 |
this.mCanonicalRepresentation = REPRESENTATION_DECODED; |
| 2012 |
this.encoded = NotCachedHolder.NOT_CACHED; |
| 2013 |
this.decoded = decoded; |
| 2014 |
} else { |
| 2015 |
throw new IllegalArgumentException("Neither encoded nor decoded"); |
| 2016 |
} |
| 2017 |
} |
| 2018 |
|
| 2019 |
abstract String getEncoded(); |
| 2020 |
|
| 2021 |
final String getDecoded() { |
| 2022 |
@SuppressWarnings("StringEquality") |
| 2023 |
boolean hasDecoded = decoded != NotCachedHolder.NOT_CACHED; |
| 2024 |
return hasDecoded ? decoded : (decoded = decode(encoded)); |
| 2025 |
} |
| 2026 |
|
| 2027 |
final void writeTo(Parcel parcel) { |
| 2028 |
final String canonicalValue; |
| 2029 |
if (mCanonicalRepresentation == REPRESENTATION_ENCODED) { |
| 2030 |
canonicalValue = encoded; |
| 2031 |
} else if (mCanonicalRepresentation == REPRESENTATION_DECODED) { |
| 2032 |
canonicalValue = decoded; |
| 2033 |
} else { |
| 2034 |
throw new IllegalArgumentException("Unknown representation: " |
| 2035 |
+ mCanonicalRepresentation); |
| 2036 |
} |
| 2037 |
if (canonicalValue == NotCachedHolder.NOT_CACHED) { |
| 2038 |
throw new AssertionError("Canonical value not cached (" |
| 2039 |
+ mCanonicalRepresentation + ")"); |
| 2040 |
} |
| 2041 |
parcel.writeInt(mCanonicalRepresentation); |
| 2042 |
parcel.writeString8(canonicalValue); |
| 2043 |
} |
| 2044 |
} |
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
static class Part extends AbstractPart { |
| 2051 |
|
| 2052 |
|
| 2053 |
static final Part NULL = new EmptyPart(null); |
| 2054 |
|
| 2055 |
|
| 2056 |
static final Part EMPTY = new EmptyPart(""); |
| 2057 |
|
| 2058 |
private Part(String encoded, String decoded) { |
| 2059 |
super(encoded, decoded); |
| 2060 |
} |
| 2061 |
|
| 2062 |
boolean isEmpty() { |
| 2063 |
return false; |
| 2064 |
} |
| 2065 |
|
| 2066 |
String getEncoded() { |
| 2067 |
@SuppressWarnings("StringEquality") |
| 2068 |
boolean hasEncoded = encoded != NotCachedHolder.NOT_CACHED; |
| 2069 |
return hasEncoded ? encoded : (encoded = encode(decoded)); |
| 2070 |
} |
| 2071 |
|
| 2072 |
static Part readFrom(Parcel parcel) { |
| 2073 |
int representation = parcel.readInt(); |
| 2074 |
String value = parcel.readString8(); |
| 2075 |
switch (representation) { |
| 2076 |
case REPRESENTATION_ENCODED: |
| 2077 |
return fromEncoded(value); |
| 2078 |
case REPRESENTATION_DECODED: |
| 2079 |
return fromDecoded(value); |
| 2080 |
default: |
| 2081 |
throw new IllegalArgumentException("Unknown representation: " |
| 2082 |
+ representation); |
| 2083 |
} |
| 2084 |
} |
| 2085 |
|
| 2086 |
|
| 2087 |
|
| 2088 |
|
| 2089 |
static Part nonNull(Part part) { |
| 2090 |
return part == null ? NULL : part; |
| 2091 |
} |
| 2092 |
|
| 2093 |
|
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 2097 |
|
| 2098 |
static Part fromEncoded(String encoded) { |
| 2099 |
return from(encoded, NotCachedHolder.NOT_CACHED); |
| 2100 |
} |
| 2101 |
|
| 2102 |
|
| 2103 |
|
| 2104 |
|
| 2105 |
|
| 2106 |
|
| 2107 |
static Part fromDecoded(String decoded) { |
| 2108 |
return from(NotCachedHolder.NOT_CACHED, decoded); |
| 2109 |
} |
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
static Part from(String encoded, String decoded) { |
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
if (encoded == null) { |
| 2122 |
return NULL; |
| 2123 |
} |
| 2124 |
if (encoded.length() == 0) { |
| 2125 |
return EMPTY; |
| 2126 |
} |
| 2127 |
|
| 2128 |
if (decoded == null) { |
| 2129 |
return NULL; |
| 2130 |
} |
| 2131 |
if (decoded .length() == 0) { |
| 2132 |
return EMPTY; |
| 2133 |
} |
| 2134 |
|
| 2135 |
return new Part(encoded, decoded); |
| 2136 |
} |
| 2137 |
|
| 2138 |
private static class EmptyPart extends Part { |
| 2139 |
public EmptyPart(String value) { |
| 2140 |
super(value, value); |
| 2141 |
if (value != null && !value.isEmpty()) { |
| 2142 |
throw new IllegalArgumentException("Expected empty value, got: " + value); |
| 2143 |
} |
| 2144 |
|
| 2145 |
encoded = decoded = value; |
| 2146 |
} |
| 2147 |
|
| 2148 |
@Override |
| 2149 |
boolean isEmpty() { |
| 2150 |
return true; |
| 2151 |
} |
| 2152 |
} |
| 2153 |
} |
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
static class PathPart extends AbstractPart { |
| 2160 |
|
| 2161 |
|
| 2162 |
static final PathPart NULL = new PathPart(null, null); |
| 2163 |
|
| 2164 |
|
| 2165 |
static final PathPart EMPTY = new PathPart("", ""); |
| 2166 |
|
| 2167 |
private PathPart(String encoded, String decoded) { |
| 2168 |
super(encoded, decoded); |
| 2169 |
} |
| 2170 |
|
| 2171 |
String getEncoded() { |
| 2172 |
@SuppressWarnings("StringEquality") |
| 2173 |
boolean hasEncoded = encoded != NotCachedHolder.NOT_CACHED; |
| 2174 |
|
| 2175 |
|
| 2176 |
return hasEncoded ? encoded : (encoded = encode(decoded, "/")); |
| 2177 |
} |
| 2178 |
|
| 2179 |
|
| 2180 |
|
| 2181 |
|
| 2182 |
|
| 2183 |
private PathSegments pathSegments; |
| 2184 |
|
| 2185 |
|
| 2186 |
|
| 2187 |
|
| 2188 |
|
| 2189 |
|
| 2190 |
|
| 2191 |
PathSegments getPathSegments() { |
| 2192 |
if (pathSegments != null) { |
| 2193 |
return pathSegments; |
| 2194 |
} |
| 2195 |
|
| 2196 |
String path = getEncoded(); |
| 2197 |
if (path == null) { |
| 2198 |
return pathSegments = PathSegments.EMPTY; |
| 2199 |
} |
| 2200 |
|
| 2201 |
PathSegmentsBuilder segmentBuilder = new PathSegmentsBuilder(); |
| 2202 |
|
| 2203 |
int previous = 0; |
| 2204 |
int current; |
| 2205 |
while ((current = path.indexOf('/', previous)) > -1) { |
| 2206 |
|
| 2207 |
|
| 2208 |
if (previous < current) { |
| 2209 |
String decodedSegment |
| 2210 |
= decode(path.substring(previous, current)); |
| 2211 |
segmentBuilder.add(decodedSegment); |
| 2212 |
} |
| 2213 |
previous = current + 1; |
| 2214 |
} |
| 2215 |
|
| 2216 |
|
| 2217 |
if (previous < path.length()) { |
| 2218 |
segmentBuilder.add(decode(path.substring(previous))); |
| 2219 |
} |
| 2220 |
|
| 2221 |
return pathSegments = segmentBuilder.build(); |
| 2222 |
} |
| 2223 |
|
| 2224 |
static PathPart appendEncodedSegment(PathPart oldPart, |
| 2225 |
String newSegment) { |
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
if (oldPart == null) { |
| 2230 |
|
| 2231 |
return fromEncoded("/" + newSegment); |
| 2232 |
} |
| 2233 |
|
| 2234 |
String oldPath = oldPart.getEncoded(); |
| 2235 |
|
| 2236 |
if (oldPath == null) { |
| 2237 |
oldPath = ""; |
| 2238 |
} |
| 2239 |
|
| 2240 |
int oldPathLength = oldPath.length(); |
| 2241 |
String newPath; |
| 2242 |
if (oldPathLength == 0) { |
| 2243 |
|
| 2244 |
newPath = "/" + newSegment; |
| 2245 |
} else if (oldPath.charAt(oldPathLength - 1) == '/') { |
| 2246 |
newPath = oldPath + newSegment; |
| 2247 |
} else { |
| 2248 |
newPath = oldPath + "/" + newSegment; |
| 2249 |
} |
| 2250 |
|
| 2251 |
return fromEncoded(newPath); |
| 2252 |
} |
| 2253 |
|
| 2254 |
static PathPart appendDecodedSegment(PathPart oldPart, String decoded) { |
| 2255 |
String encoded = encode(decoded); |
| 2256 |
|
| 2257 |
|
| 2258 |
return appendEncodedSegment(oldPart, encoded); |
| 2259 |
} |
| 2260 |
|
| 2261 |
static PathPart readFrom(Parcel parcel) { |
| 2262 |
int representation = parcel.readInt(); |
| 2263 |
switch (representation) { |
| 2264 |
case REPRESENTATION_ENCODED: |
| 2265 |
return fromEncoded(parcel.readString8()); |
| 2266 |
case REPRESENTATION_DECODED: |
| 2267 |
return fromDecoded(parcel.readString8()); |
| 2268 |
default: |
| 2269 |
throw new IllegalArgumentException("Unknown representation: " + representation); |
| 2270 |
} |
| 2271 |
} |
| 2272 |
|
| 2273 |
static PathPart readFrom(boolean hasSchemeOrAuthority, Parcel parcel) { |
| 2274 |
final PathPart path = readFrom(parcel); |
| 2275 |
return hasSchemeOrAuthority ? makeAbsolute(path) : path; |
| 2276 |
} |
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
static PathPart fromEncoded(String encoded) { |
| 2284 |
return from(encoded, NotCachedHolder.NOT_CACHED); |
| 2285 |
} |
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
static PathPart fromDecoded(String decoded) { |
| 2293 |
return from(NotCachedHolder.NOT_CACHED, decoded); |
| 2294 |
} |
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
|
| 2302 |
static PathPart from(String encoded, String decoded) { |
| 2303 |
if (encoded == null) { |
| 2304 |
return NULL; |
| 2305 |
} |
| 2306 |
|
| 2307 |
if (encoded.length() == 0) { |
| 2308 |
return EMPTY; |
| 2309 |
} |
| 2310 |
|
| 2311 |
return new PathPart(encoded, decoded); |
| 2312 |
} |
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
static PathPart makeAbsolute(PathPart oldPart) { |
| 2319 |
@SuppressWarnings("StringEquality") |
| 2320 |
boolean encodedCached = oldPart.encoded != NotCachedHolder.NOT_CACHED; |
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
String oldPath = encodedCached ? oldPart.encoded : oldPart.decoded; |
| 2325 |
|
| 2326 |
if (oldPath == null || oldPath.length() == 0 |
| 2327 |
|| oldPath.startsWith("/")) { |
| 2328 |
return oldPart; |
| 2329 |
} |
| 2330 |
|
| 2331 |
|
| 2332 |
String newEncoded = encodedCached |
| 2333 |
? "/" + oldPart.encoded : NotCachedHolder.NOT_CACHED; |
| 2334 |
|
| 2335 |
|
| 2336 |
@SuppressWarnings("StringEquality") |
| 2337 |
boolean decodedCached = oldPart.decoded != NotCachedHolder.NOT_CACHED; |
| 2338 |
String newDecoded = decodedCached |
| 2339 |
? "/" + oldPart.decoded |
| 2340 |
: NotCachedHolder.NOT_CACHED; |
| 2341 |
|
| 2342 |
return new PathPart(newEncoded, newDecoded); |
| 2343 |
} |
| 2344 |
} |
| 2345 |
|
| 2346 |
|
| 2347 |
|
| 2348 |
|
| 2349 |
|
| 2350 |
|
| 2351 |
|
| 2352 |
|
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
public static Uri withAppendedPath(Uri baseUri, String pathSegment) { |
| 2357 |
Builder builder = baseUri.buildUpon(); |
| 2358 |
builder = builder.appendEncodedPath(pathSegment); |
| 2359 |
return builder.build(); |
| 2360 |
} |
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
|
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
|
| 2370 |
@UnsupportedAppUsage |
| 2371 |
public Uri getCanonicalUri() { |
| 2372 |
if ("file".equals(getScheme())) { |
| 2373 |
final String canonicalPath; |
| 2374 |
try { |
| 2375 |
canonicalPath = new File(getPath()).getCanonicalPath(); |
| 2376 |
} catch (IOException e) { |
| 2377 |
return this; |
| 2378 |
} |
| 2379 |
|
| 2380 |
if (Environment.isExternalStorageEmulated()) { |
| 2381 |
final String legacyPath = Environment.getLegacyExternalStorageDirectory() |
| 2382 |
.toString(); |
| 2383 |
|
| 2384 |
|
| 2385 |
if (canonicalPath.startsWith(legacyPath)) { |
| 2386 |
return Uri.fromFile(new File( |
| 2387 |
Environment.getExternalStorageDirectory().toString(), |
| 2388 |
canonicalPath.substring(legacyPath.length() + 1))); |
| 2389 |
} |
| 2390 |
} |
| 2391 |
|
| 2392 |
return Uri.fromFile(new File(canonicalPath)); |
| 2393 |
} else { |
| 2394 |
return this; |
| 2395 |
} |
| 2396 |
} |
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
|
| 2401 |
|
| 2402 |
|
| 2403 |
|
| 2404 |
public void checkFileUriExposed(String location) { |
| 2405 |
if ("file".equals(getScheme()) |
| 2406 |
&& (getPath() != null) && !getPath().startsWith("/system/")) { |
| 2407 |
StrictMode.onFileUriExposed(this, location); |
| 2408 |
} |
| 2409 |
} |
| 2410 |
|
| 2411 |
|
| 2412 |
|
| 2413 |
|
| 2414 |
|
| 2415 |
|
| 2416 |
|
| 2417 |
public void checkContentUriWithoutPermission(String location, int flags) { |
| 2418 |
if ("content".equals(getScheme()) && !Intent.isAccessUriMode(flags)) { |
| 2419 |
StrictMode.onContentUriWithoutPermission(this, location); |
| 2420 |
} |
| 2421 |
} |
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
|
| 2426 |
|
| 2427 |
|
| 2428 |
|
| 2429 |
public boolean isPathPrefixMatch(Uri prefix) { |
| 2430 |
if (!Objects.equals(getScheme(), prefix.getScheme())) return false; |
| 2431 |
if (!Objects.equals(getAuthority(), prefix.getAuthority())) return false; |
| 2432 |
|
| 2433 |
List<String> seg = getPathSegments(); |
| 2434 |
List<String> prefixSeg = prefix.getPathSegments(); |
| 2435 |
|
| 2436 |
final int prefixSize = prefixSeg.size(); |
| 2437 |
if (seg.size() < prefixSize) return false; |
| 2438 |
|
| 2439 |
for (int i = 0; i < prefixSize; i++) { |
| 2440 |
if (!Objects.equals(seg.get(i), prefixSeg.get(i))) { |
| 2441 |
return false; |
| 2442 |
} |
| 2443 |
} |
| 2444 |
|
| 2445 |
return true; |
| 2446 |
} |
| 2447 |
} |