summaryrefslogtreecommitdiffstats
path: root/core/java/android/net
diff options
context:
space:
mode:
authorNick Pelly <npelly@google.com>2012-01-09 14:12:58 -0800
committerNick Pelly <npelly@google.com>2012-01-24 20:04:13 -0800
commitccae412deda8b0c165c86f395752c0667a3411a6 (patch)
tree3ddc03df0182a5b372c2d38e27d5a7c312cf9e31 /core/java/android/net
parent07d7d5a22dbb0a8df5631c8014f4706dd1e449da (diff)
downloadframeworks_base-ccae412deda8b0c165c86f395752c0667a3411a6.zip
frameworks_base-ccae412deda8b0c165c86f395752c0667a3411a6.tar.gz
frameworks_base-ccae412deda8b0c165c86f395752c0667a3411a6.tar.bz2
Add API's for normalizing MIME's and URI's.
Helps developers create well-behaved intents: - lower case MIME data type - strip parameters from MIME content types - lowercase URI scheme The new API's are normalizeAndSetType() normalizeAndSetData() normalizeAndSetDataAndType() Uri.normalize() normalizeMimeType() Change-Id: Ib5c907897f39b1f705bcc4c9103ba1e6f316380b
Diffstat (limited to 'core/java/android/net')
-rw-r--r--core/java/android/net/Uri.java33
1 files changed, 33 insertions, 0 deletions
diff --git a/core/java/android/net/Uri.java b/core/java/android/net/Uri.java
index 0fb49bc..defe7aa 100644
--- a/core/java/android/net/Uri.java
+++ b/core/java/android/net/Uri.java
@@ -28,6 +28,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Locale;
import java.util.RandomAccess;
import java.util.Set;
import libcore.net.UriCodec;
@@ -1716,6 +1717,38 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
return (!"false".equals(flag) && !"0".equals(flag));
}
+ /**
+ * Return a normalized representation of this Uri.
+ *
+ * <p>A normalized Uri has a lowercase scheme component.
+ * This aligns the Uri with Android best practices for
+ * intent filtering.
+ *
+ * <p>For example, "HTTP://www.android.com" becomes
+ * "http://www.android.com"
+ *
+ * <p>All URIs received from outside Android (such as user input,
+ * or external sources like Bluetooth, NFC, or the Internet) should
+ * be normalized before they are used to create an Intent.
+ *
+ * <p class="note">This method does <em>not</em> validate bad URI's,
+ * or 'fix' poorly formatted URI's - so do not use it for input validation.
+ * A Uri will always be returned, even if the Uri is badly formatted to
+ * begin with and a scheme component cannot be found.
+ *
+ * @return normalized Uri (never null)
+ * @see {@link android.content.Intent#setData}
+ * @see {@link #setNormalizedData}
+ */
+ public Uri normalize() {
+ String scheme = getScheme();
+ if (scheme == null) return this; // give up
+ String lowerScheme = scheme.toLowerCase(Locale.US);
+ if (scheme.equals(lowerScheme)) return this; // no change
+
+ return buildUpon().scheme(lowerScheme).build();
+ }
+
/** Identifies a null parcelled Uri. */
private static final int NULL_TYPE_ID = 0;