Bug 1038793 - Minimize unused space in WebView of search activity. r=margaret
authorEric Edens <eedens@mozilla.com>
Wed, 16 Jul 2014 17:08:22 -0700
changeset 216578 4034177d4227c87e1c97e163cb5d1ab165d6330a
parent 216577 b3fb226d9f9cfe7c7a011cc84ebf85287b43eb6b
child 216579 661a00579db9709b21dc72a6d63073f04452b511
push id515
push userraliiev@mozilla.com
push dateMon, 06 Oct 2014 12:51:51 +0000
treeherdermozilla-release@267c7a481bef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs1038793
milestone33.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 1038793 - Minimize unused space in WebView of search activity. r=margaret
mobile/android/search/java/org/mozilla/search/PostSearchFragment.java
mobile/android/search/res/layout/search_activity_main.xml
mobile/android/search/res/values/search_dimens.xml
--- a/mobile/android/search/java/org/mozilla/search/PostSearchFragment.java
+++ b/mobile/android/search/java/org/mozilla/search/PostSearchFragment.java
@@ -7,47 +7,42 @@ package org.mozilla.search;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.webkit.WebChromeClient;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 
 public class PostSearchFragment extends Fragment {
 
     private static final String LOGTAG = "PostSearchFragment";
     private WebView webview;
 
-    public PostSearchFragment() {}
+    private static String HIDE_BANNER_SCRIPT = "javascript:(function(){var tag=document.createElement('style');" +
+            "tag.type='text/css';document.getElementsByTagName('head')[0].appendChild(tag);tag.innerText='#nav,#header{display:none}'})();";
+
+    public PostSearchFragment() {
+    }
 
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
-        View mainView = inflater.inflate(R.layout.search_activity_detail, container, false);
-
+        final View mainView = inflater.inflate(R.layout.search_activity_detail, container, false);
 
         webview = (WebView) mainView.findViewById(R.id.webview);
-        webview.setWebViewClient(new WebViewClient(){
-            @Override
-            public void onPageStarted(WebView view, String url, Bitmap favicon) {
-                if (isSearchResultsPage(url)) {
-                    super.onPageStarted(view, url, favicon);
-                } else {
-                    webview.stopLoading();
-                    Intent i = new Intent(Intent.ACTION_VIEW);
-                    i.setData(Uri.parse(url));
-                    startActivity(i);
-                }
-            }
-        });
+        webview.setWebViewClient(new LinkInterceptingClient());
+        webview.setWebChromeClient(new StyleInjectingClient());
+        webview.getSettings().setJavaScriptEnabled(true);
+
         return mainView;
     }
 
     /**
      * Test if a given URL is a page of search results.
      * <p>
      * Search results pages will be shown in the embedded view.  Other pages are
      * opened in external browsers.
@@ -61,9 +56,46 @@ public class PostSearchFragment extends 
 
     public void startSearch(String query) {
         setUrl(Constants.YAHOO_WEB_SEARCH_BASE_URL + Uri.encode(query));
     }
 
     public void setUrl(String url) {
         webview.loadUrl(url);
     }
+
+    /**
+     * A custom WebViewClient that intercepts every page load. This allows
+     * us to decide whether to load the url here, or send it to Android
+     * as an intent.
+     */
+    private class LinkInterceptingClient extends WebViewClient {
+
+        @Override
+        public void onPageStarted(WebView view, String url, Bitmap favicon) {
+            if (isSearchResultsPage(url)) {
+                super.onPageStarted(view, url, favicon);
+            } else {
+                view.stopLoading();
+                Intent i = new Intent(Intent.ACTION_VIEW);
+                i.setData(Uri.parse(url));
+                startActivity(i);
+            }
+        }
+    }
+
+    /**
+     * A custom WebChromeClient that allows us to inject CSS into
+     * the head of the HTML.
+     *
+     * We use the WebChromeClient because it provides a hook to the titleReceived
+     * event. Once the title is available, the page will have started parsing the
+     * head element. The script injects its CSS into the head element.
+     */
+    private class StyleInjectingClient extends WebChromeClient {
+
+        @Override
+        public void onReceivedTitle(WebView view, String title) {
+            super.onReceivedTitle(view, title);
+            view.loadUrl(HIDE_BANNER_SCRIPT);
+        }
+    }
 }
--- a/mobile/android/search/res/layout/search_activity_main.xml
+++ b/mobile/android/search/res/layout/search_activity_main.xml
@@ -9,16 +9,17 @@
     android:layout_height="match_parent"
     android:orientation="vertical"
     tools:context=".MainActivity">
 
     <fragment
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:name="org.mozilla.search.PostSearchFragment"
+        android:layout_marginTop="@dimen/webview_height_offset"
         android:id="@+id/postsearch"
         />
 
     <fragment
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:name="org.mozilla.search.PreSearchFragment"
         android:id="@+id/presearch"
--- a/mobile/android/search/res/values/search_dimens.xml
+++ b/mobile/android/search/res/values/search_dimens.xml
@@ -1,9 +1,10 @@
 <!-- This Source Code Form is subject to the terms of the Mozilla Public
    - License, v. 2.0. If a copy of the MPL was not distributed with this
    - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
 
 <resources>
-    <!-- Default screen margins, per the Android Design guidelines. -->
-    <dimen name="activity_horizontal_margin">16dp</dimen>
-    <dimen name="activity_vertical_margin">16dp</dimen>
+    <!--This is used to offset the webview so that it is not covered-->
+    <!--by the search bar. If we change the height of the search bar-->
+    <!--then this will need to be updated.-->
+    <dimen name="webview_height_offset">55dp</dimen>
 </resources>