Bug 1035131 - Factor out methods to encode/decode user-entered:// URLs. r=mfinkle, a=lmandel
authorLucas Rocha <lucasr@mozilla.com>
Mon, 07 Jul 2014 15:59:52 +0100
changeset 207882 2ffb6fe3448419d9680bce6930b117080088c45f
parent 207881 8697de0fdca324528854080ac308c58e428a1c1e
child 207883 5ece78da865bca6cc7aa14a3798f75de66230bad
push id3741
push userasasaki@mozilla.com
push dateMon, 21 Jul 2014 20:25:18 +0000
treeherdermozilla-beta@4d6f46f5af68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle, lmandel
bugs1035131
milestone32.0a2
Bug 1035131 - Factor out methods to encode/decode user-entered:// URLs. r=mfinkle, a=lmandel
mobile/android/base/home/HomeFragment.java
mobile/android/base/home/PinSiteDialog.java
mobile/android/base/home/TopSitesGridView.java
mobile/android/base/home/TopSitesPanel.java
mobile/android/base/util/StringUtils.java
--- a/mobile/android/base/home/HomeFragment.java
+++ b/mobile/android/base/home/HomeFragment.java
@@ -186,17 +186,17 @@ abstract class HomeFragment extends Frag
             }
 
             Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, TelemetryContract.Method.CONTEXT_MENU);
 
             final String url = (info.isInReadingList() ? ReaderModeUtils.getAboutReaderForUrl(info.url) : info.url);
 
             // Some pinned site items have "user-entered" urls. URLs entered in the PinSiteDialog are wrapped in
             // a special URI until we can get a valid URL. If the url is a user-entered url, decode the URL before loading it.
-            final Tab newTab = Tabs.getInstance().loadUrl(decodeUserEnteredUrl(url), flags);
+            final Tab newTab = Tabs.getInstance().loadUrl(StringUtils.decodeUserEnteredUrl(url), flags);
             final String message = isPrivate ?
                     getResources().getString(R.string.new_private_tab_opened) :
                     getResources().getString(R.string.new_tab_opened);
             // Bug 1023407: ButtonToasts interact badly with touch events, blocking interaction with
             // the rest of the system until the ButtonToast expires or is otherwise dismissed (Bug
             // 1019735). Until this is fixed, we'll just use regular system Toasts.
             Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
             return true;
@@ -252,32 +252,16 @@ abstract class HomeFragment extends Frag
         mCanLoadHint = canLoadHint;
         loadIfVisible();
     }
 
     boolean getCanLoadHint() {
         return mCanLoadHint;
     }
 
-    /**
-     * Given a url with a user-entered scheme, extract the
-     * scheme-specific component. For e.g, given "user-entered://www.google.com",
-     * this method returns "//www.google.com". If the passed url
-     * does not have a user-entered scheme, the same url will be returned.
-     *
-     * @param  url to be decoded
-     * @return url component entered by user
-     */
-    public static String decodeUserEnteredUrl(String url) {
-        Uri uri = Uri.parse(url);
-        if ("user-entered".equals(uri.getScheme())) {
-            return uri.getSchemeSpecificPart();
-        }
-        return url;
-    }
 
     protected abstract void load();
 
     protected boolean canLoad() {
         return (mCanLoadHint && isVisible() && getUserVisibleHint());
     }
 
     protected void loadIfVisible() {
--- a/mobile/android/base/home/PinSiteDialog.java
+++ b/mobile/android/base/home/PinSiteDialog.java
@@ -5,16 +5,17 @@
 
 package org.mozilla.gecko.home;
 
 import java.util.EnumSet;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserDB.FilterFlags;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
+import org.mozilla.gecko.util.StringUtils;
 
 import android.content.Context;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.app.DialogFragment;
 import android.support.v4.app.LoaderManager;
 import android.support.v4.app.LoaderManager.LoaderCallbacks;
 import android.support.v4.content.Loader;
@@ -109,17 +110,17 @@ class PinSiteDialog extends DialogFragme
                 if (keyCode != KeyEvent.KEYCODE_ENTER || mOnSiteSelectedListener == null) {
                     return false;
                 }
 
                 // If the user manually entered a search term or URL, wrap the value in
                 // a special URI until we can get a valid URL for this bookmark.
                 final String text = mSearch.getText().toString().trim();
                 if (!TextUtils.isEmpty(text)) {
-                    final String url = TopSitesPanel.encodeUserEnteredUrl(text);
+                    final String url = StringUtils.encodeUserEnteredUrl(text);
                     mOnSiteSelectedListener.onSiteSelected(url, text);
                     dismiss();
                 }
 
                 return true;
             }
         });
 
--- a/mobile/android/base/home/TopSitesGridView.java
+++ b/mobile/android/base/home/TopSitesGridView.java
@@ -9,16 +9,17 @@ import java.util.EnumSet;
 
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.ThumbnailHelper;
 import org.mozilla.gecko.db.BrowserContract.TopSites;
 import org.mozilla.gecko.db.TopSitesCursorWrapper;
 import org.mozilla.gecko.home.HomePager.OnUrlOpenListener;
+import org.mozilla.gecko.util.StringUtils;
 
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.database.Cursor;
 import android.graphics.Rect;
 import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.ContextMenu.ContextMenuInfo;
@@ -101,17 +102,17 @@ public class TopSitesGridView extends Gr
         super.onAttachedToWindow();
 
         setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 TopSitesGridItemView item = (TopSitesGridItemView) view;
 
                 // Decode "user-entered" URLs before loading them.
-                String url = HomeFragment.decodeUserEnteredUrl(item.getUrl());
+                String url = StringUtils.decodeUserEnteredUrl(item.getUrl());
                 int type = item.getType();
 
                 // If the url is empty, the user can pin a site.
                 // If not, navigate to the page given by the url.
                 if (type != TopSites.TYPE_BLANK) {
                     if (mUrlOpenListener != null) {
                         final TelemetryContract.Method method;
                         if (type == TopSites.TYPE_SUGGESTED) {
--- a/mobile/android/base/home/TopSitesPanel.java
+++ b/mobile/android/base/home/TopSitesPanel.java
@@ -345,17 +345,18 @@ public class TopSitesPanel extends HomeF
 
             Telemetry.sendUIEvent(TelemetryContract.Event.UNPIN);
 
             return true;
         }
 
         if (itemId == R.id.top_sites_edit) {
             // Decode "user-entered" URLs before showing them.
-            mEditPinnedSiteListener.onEditPinnedSite(info.position, decodeUserEnteredUrl(info.url));
+            mEditPinnedSiteListener.onEditPinnedSite(info.position,
+                                                     StringUtils.decodeUserEnteredUrl(info.url));
 
             Telemetry.sendUIEvent(TelemetryContract.Event.EDIT);
             return true;
         }
 
         return false;
     }
 
@@ -367,20 +368,16 @@ public class TopSitesPanel extends HomeF
         // visited, we want to load it as quickly as possible. Heavy load on
         // the Gecko thread can slow down the time it takes for thumbnails to
         // appear, especially during startup (bug 897162). By minimizing the
         // Gecko thread priority, we ensure that the UI appears quickly. The
         // priority is reset to normal once thumbnails are loaded.
         ThreadUtils.reduceGeckoPriority(PRIORITY_RESET_TIMEOUT);
     }
 
-    static String encodeUserEnteredUrl(String url) {
-        return Uri.fromParts("user-entered", url, null).toString();
-    }
-
     /**
      * Listener for editing pinned sites.
      */
     private class EditPinnedSiteListener implements OnEditPinnedSiteListener,
                                                     OnSiteSelectedListener {
         // Tag for the PinSiteDialog fragment.
         private static final String TAG_PIN_SITE = "pin_site";
 
--- a/mobile/android/base/util/StringUtils.java
+++ b/mobile/android/base/util/StringUtils.java
@@ -149,9 +149,30 @@ public class StringUtils {
 
     public static String getFilterFromUrl(String url) {
         if (TextUtils.isEmpty(url)) {
             return null;
         }
 
         return url.substring(FILTER_URL_PREFIX.length());
     }
+
+    /**
+     * Given a url with a user-entered scheme, extract the
+     * scheme-specific component. For e.g, given "user-entered://www.google.com",
+     * this method returns "//www.google.com". If the passed url
+     * does not have a user-entered scheme, the same url will be returned.
+     *
+     * @param  url to be decoded
+     * @return url component entered by user
+     */
+    public static String decodeUserEnteredUrl(String url) {
+        Uri uri = Uri.parse(url);
+        if ("user-entered".equals(uri.getScheme())) {
+            return uri.getSchemeSpecificPart();
+        }
+        return url;
+    }
+
+    public static String encodeUserEnteredUrl(String url) {
+        return Uri.fromParts("user-entered", url, null).toString();
+    }
 }