Bug 839580 - getOpenURIIntent uses an API level 16 method. r=bnicholson
authorRichard Newman <rnewman@mozilla.com>
Fri, 08 Feb 2013 15:30:28 -0800
changeset 131285 abf8199243c844d738a59fa422a19a9bf3cb335d
parent 131284 ba16ce3b8bc113c4d5ca1e1caecbe50dd52334bc
child 131287 02430bf449586aec509a45dc5389414df5c5c7d1
push id2323
push userbbajaj@mozilla.com
push dateMon, 01 Apr 2013 19:47:02 +0000
treeherdermozilla-beta@7712be144d91 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson
bugs839580
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 839580 - getOpenURIIntent uses an API level 16 method. r=bnicholson
mobile/android/base/GeckoAppShell.java
--- a/mobile/android/base/GeckoAppShell.java
+++ b/mobile/android/base/GeckoAppShell.java
@@ -81,16 +81,17 @@ import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.lang.reflect.Field;
 import java.net.URL;
 import java.nio.ByteBuffer;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Locale;
 import java.util.NoSuchElementException;
 import java.util.StringTokenizer;
 import java.util.concurrent.SynchronousQueue;
 import java.net.ProxySelector;
 import java.net.Proxy;
 import java.net.URI;
 
 public class GeckoAppShell
@@ -1000,16 +1001,35 @@ public class GeckoAppShell
             context.startActivity(intent);
             return true;
         } catch (ActivityNotFoundException e) {
             return false;
         }
     }
 
     /**
+     * Return a <code>Uri</code> instance which is equivalent to <code>u</code>,
+     * but with a guaranteed-lowercase scheme as if the API level 16 method
+     * <code>u.normalizeScheme</code> had been called.
+     *
+     * @param u the <code>Uri</code> to normalize.
+     * @return a <code>Uri</code>, which might be <code>u</code>.
+     */
+    static Uri normalizeUriScheme(final Uri u) {
+        final String scheme = u.getScheme();
+        final String lower  = scheme.toLowerCase(Locale.US);
+        if (lower.equals(scheme)) {
+            return u;
+        }
+
+        // Otherwise, return a new URI with a normalized scheme.
+        return u.buildUpon().scheme(lower).build();
+    }
+
+    /**
      * Given a URI, a MIME type, an Android intent "action", and a title,
      * produce an intent which can be used to start an activity to open
      * the specified URI.
      *
      * @param context a <code>Context</code> instance.
      * @param targetURI the string spec of the URI to open.
      * @param mimeType an optional MIME type string.
      * @param action an Android action specifier, such as
@@ -1037,33 +1057,33 @@ public class GeckoAppShell
             if (mimeType != null && mimeType.length() > 0) {
                 shareIntent.setType(mimeType);
             }
 
             return Intent.createChooser(shareIntent,
                                         context.getResources().getString(R.string.share_title)); 
         }
 
+        final Uri uri = normalizeUriScheme(Uri.parse(targetURI));
         if (mimeType.length() > 0) {
             Intent intent = getIntentForActionString(action);
-            intent.setDataAndType(Uri.parse(targetURI), mimeType);
+            intent.setDataAndType(uri, mimeType);
             return intent;
         }
 
-        final Uri uri = Uri.parse(targetURI);
         if (!isUriSafeForScheme(uri)) {
             return null;
         }
-        
+
         final String scheme = uri.getScheme();
         final Intent intent = getIntentForActionString(action);
 
         // Start with the original URI. If we end up modifying it,
         // we'll overwrite it.
-        intent.setDataAndNormalize(uri);
+        intent.setData(uri);
 
         // Have a special handling for the SMS, as the message body
         // is not extracted from the URI automatically.
         if (!"sms".equals(scheme)) {
             return intent;
         }
 
         final String query = uri.getEncodedQuery();
@@ -1088,20 +1108,19 @@ public class GeckoAppShell
 
         if (!foundBody) {
             // No need to rewrite the URI, then.
             return intent;
         }
 
         // Form a new URI without the body field in the query part, and
         // push that into the new Intent.
-        final String prefix = targetURI.substring(0, targetURI.indexOf('?'));
         final String newQuery = resultQuery.length() > 0 ? "?" + resultQuery : "";
-        final Uri pruned = Uri.parse(prefix + newQuery);
-        intent.setDataAndNormalize(pruned);
+        final Uri pruned = uri.buildUpon().encodedQuery(newQuery).build();
+        intent.setData(pruned);
 
         return intent;
     }
 
     static SynchronousQueue<String> sClipboardQueue =
         new SynchronousQueue<String>();
     private static String EMPTY_STRING = new String();