Bug 1029292 - Replace Search Activity's GeckoView with WebView. r=nalexander

This commit is contained in:
Eric Edens 2014-07-14 22:16:25 -07:00
parent b47d2fdc5d
commit 74be753bbe
5 changed files with 49 additions and 81 deletions

View File

@ -21,4 +21,7 @@ public class Constants {
public static final String SEARCH_FRAGMENT = "org.mozilla.search.SEARCH_FRAGMENT";
public static final String AUTOCOMPLETE_ROW_LIMIT = "5";
public static final String YAHOO_WEB_SEARCH_BASE_URL = "https://search.yahoo.com/search?p=";
public static final String YAHOO_WEB_SEARCH_RESULTS_FILTER = "//search.yahoo.com";
}

View File

@ -39,14 +39,14 @@ public class MainActivity extends FragmentActivity implements AcceptsSearchQuery
@Override
public void onSearch(String s) {
startPostsearch();
((PostSearchFragment) getSupportFragmentManager().findFragmentById(R.id.gecko))
.setUrl("https://search.yahoo.com/search?p=" + Uri.encode(s));
((PostSearchFragment) getSupportFragmentManager().findFragmentById(R.id.postsearch))
.startSearch(s);
}
private void startPresearch() {
if (state != State.PRESEARCH) {
state = State.PRESEARCH;
findViewById(R.id.gecko).setVisibility(View.INVISIBLE);
findViewById(R.id.postsearch).setVisibility(View.INVISIBLE);
findViewById(R.id.presearch).setVisibility(View.VISIBLE);
}
}
@ -55,7 +55,7 @@ public class MainActivity extends FragmentActivity implements AcceptsSearchQuery
if (state != State.POSTSEARCH) {
state = State.POSTSEARCH;
findViewById(R.id.presearch).setVisibility(View.INVISIBLE);
findViewById(R.id.gecko).setVisibility(View.VISIBLE);
findViewById(R.id.postsearch).setVisibility(View.VISIBLE);
}
}

View File

@ -5,23 +5,22 @@
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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.mozilla.gecko.GeckoView;
import org.mozilla.gecko.GeckoViewChrome;
import org.mozilla.gecko.GeckoViewContent;
import org.mozilla.gecko.PrefsHelper;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class PostSearchFragment extends Fragment {
private static final String LOGTAG = "PostSearchFragment";
private GeckoView geckoView;
private WebView webview;
public PostSearchFragment() {}
@Override
@ -30,71 +29,41 @@ public class PostSearchFragment extends Fragment {
View mainView = inflater.inflate(R.layout.search_activity_detail, container, false);
geckoView = (GeckoView) mainView.findViewById(R.id.gecko_view);
geckoView.setChromeDelegate(new MyGeckoViewChrome());
geckoView.setContentDelegate(new SearchGeckoView());
PrefsHelper.setPref("privacy.clearOnShutdown.cache", true);
PrefsHelper.setPref("privacy.clearOnShutdown.cookies", true);
if (null == geckoView.getCurrentBrowser()) {
// This pageload allows Fennec to be loaded in a background fragment.
// Without supplying a URL, it doesn't look like Fennec will get loaded?
geckoView.addBrowser("https://search.yahoo.com/search?p=firefox%20android");
}
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);
}
}
});
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.
*
* @param url to test.
* @return true if <code>url</code> is a page of search results.
*/
protected boolean isSearchResultsPage(String url) {
return url.contains(Constants.YAHOO_WEB_SEARCH_RESULTS_FILTER);
}
public void startSearch(String query) {
setUrl(Constants.YAHOO_WEB_SEARCH_BASE_URL + Uri.encode(query));
}
public void setUrl(String url) {
if (null == geckoView.getCurrentBrowser()) {
geckoView.addBrowser(url);
} else {
geckoView.getCurrentBrowser().loadUrl(url);
}
}
private static class MyGeckoViewChrome extends GeckoViewChrome {
@Override
public void onReady(GeckoView view) {
Log.i(LOGTAG, "Gecko is ready");
PrefsHelper.setPref("devtools.debugger.remote-enabled", true);
// The Gecko libraries have finished loading and we can use the rendering engine.
// Let's add a browser (required) and load a page into it.
}
}
private class SearchGeckoView extends GeckoViewContent {
@Override
public void onPageStart(GeckoView geckoView, GeckoView.Browser browser, String s) {
Log.i("OnPageStart", s);
// Only load this page if it's the Yahoo search page that we're using.
// TODO: Make this check more robust, and allow for other search providers.
if (s.contains("//search.yahoo.com")) {
super.onPageStart(geckoView, browser, s);
} else {
browser.stop();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(s));
startActivity(i);
}
}
@Override
public void onPageShow(GeckoView geckoView, GeckoView.Browser browser) {
}
webview.loadUrl(url);
}
}

View File

@ -6,15 +6,11 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.mozilla.search.PostSearchFragment">
<org.mozilla.gecko.GeckoView
android:id="@+id/gecko_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"/>
</RelativeLayout>

View File

@ -14,7 +14,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="org.mozilla.search.PostSearchFragment"
android:id="@+id/gecko"
android:id="@+id/postsearch"
/>
<fragment