From 48944a7383fc9ca47260181fc9f8ffbc999cf5e2 Mon Sep 17 00:00:00 2001 From: timvolodine Date: Thu, 30 Apr 2015 09:50:22 -0700 Subject: [PATCH] [Android WebView] Add url bar to WebViewShell and make it visible as app. This patch adds some basic browsing functionality for manual testing. It adds a url bar and a button to the WebViewBrowserActivity to manually load urls using webview. JavaScript and geolocation are enabled by default now. BUG=481508 Review URL: https://codereview.chromium.org/1110523002 Cr-Commit-Position: refs/heads/master@{#327725} --- .../tools/WebViewShell/AndroidManifest.xml | 5 ++ .../res/drawable/breadcrumb_arrow_black.png | Bin 0 -> 295 bytes .../WebViewShell/res/layout/activity_webview.xml | 2 - .../res/layout/activity_webview_browser.xml | 37 +++++++++++ .../webview_shell/WebViewBrowserActivity.java | 70 ++++++++++++++++++--- 5 files changed, 105 insertions(+), 9 deletions(-) create mode 100644 android_webview/tools/WebViewShell/res/drawable/breadcrumb_arrow_black.png create mode 100644 android_webview/tools/WebViewShell/res/layout/activity_webview_browser.xml diff --git a/android_webview/tools/WebViewShell/AndroidManifest.xml b/android_webview/tools/WebViewShell/AndroidManifest.xml index 71479ee0a23c..1c8ab3097db6 100644 --- a/android_webview/tools/WebViewShell/AndroidManifest.xml +++ b/android_webview/tools/WebViewShell/AndroidManifest.xml @@ -15,6 +15,7 @@ + + + + + diff --git a/android_webview/tools/WebViewShell/res/drawable/breadcrumb_arrow_black.png b/android_webview/tools/WebViewShell/res/drawable/breadcrumb_arrow_black.png new file mode 100644 index 0000000000000000000000000000000000000000..7b9ff79bd3e75b6a84372fd4b5b923edf0be1613 GIT binary patch literal 295 zcwXxa@N?(olHy`uVBq!ia0vp^Dj>|k0wldT1B8JTXMsm#F#`j)FbFd;%$g$s6l5>) z^mS#w&mt|LtF>#glP^%{n5TN`irY|>8$Bg;9a)#NRI`_0(Dho7I$pJMd?*k*1$xu&;M^Mk@1 z3zxDeR_T4#`lIAm68j=FQrU0M?hC6oI?BA - - diff --git a/android_webview/tools/WebViewShell/res/layout/activity_webview_browser.xml b/android_webview/tools/WebViewShell/res/layout/activity_webview_browser.xml new file mode 100644 index 000000000000..4e9e6a496e6b --- /dev/null +++ b/android_webview/tools/WebViewShell/res/layout/activity_webview_browser.xml @@ -0,0 +1,37 @@ + + + + + + + + + + diff --git a/android_webview/tools/WebViewShell/src/org/chromium/webview_shell/WebViewBrowserActivity.java b/android_webview/tools/WebViewShell/src/org/chromium/webview_shell/WebViewBrowserActivity.java index c6436053341a..56d4fe776231 100644 --- a/android_webview/tools/WebViewShell/src/org/chromium/webview_shell/WebViewBrowserActivity.java +++ b/android_webview/tools/WebViewShell/src/org/chromium/webview_shell/WebViewBrowserActivity.java @@ -5,38 +5,94 @@ package org.chromium.webview_shell; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.view.KeyEvent; +import android.view.View; +import android.view.View.OnKeyListener; +import android.view.inputmethod.InputMethodManager; +import android.webkit.GeolocationPermissions; +import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; +import android.widget.EditText; +import android.widget.TextView; /** - * This activity is designed for URL browsing testing of WebView. It takes a URL as an argument, and - * displays the page. + * This activity is designed for starting a "mini-browser" for manual testing of WebView. + * It takes an optional URL as an argument, and displays the page. There is a URL bar + * on top of the webview for manually specifying URLs to load. */ public class WebViewBrowserActivity extends Activity { + private EditText mUrlBar; + private WebView mWebView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setTitle( getResources().getString(R.string.title_activity_browser)); - setContentView(R.layout.activity_webview); - WebView webView = (WebView) findViewById(R.id.webview); + setContentView(R.layout.activity_webview_browser); + mWebView = (WebView) findViewById(R.id.webview); + mWebView.getSettings().setJavaScriptEnabled(true); + mWebView.getSettings().setGeolocationEnabled(true); - webView.setWebViewClient(new WebViewClient() { + mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView webView, String url) { return false; } }); + mWebView.setWebChromeClient(new WebChromeClient() { + @Override + public void onGeolocationPermissionsShowPrompt(String origin, + GeolocationPermissions.Callback callback) { + callback.invoke(origin, true, false); + } + }); + + mUrlBar = (EditText) findViewById(R.id.url_field); + mUrlBar.setOnKeyListener(new OnKeyListener() { + public boolean onKey(View view, int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { + loadUrlFromUrlBar(view); + return true; + } + return false; + } + }); + String url = getUrlFromIntent(getIntent()); - webView.loadUrl(url); + if (url != null) { + mUrlBar.setText(url, TextView.BufferType.EDITABLE); + loadUrl(url); + } + } + + public void loadUrlFromUrlBar(View view) { + loadUrl(mUrlBar.getText().toString()); + hideKeyboard(mUrlBar); + } + + private void loadUrl(String url) { + mWebView.loadUrl(url); + mWebView.requestFocus(); + } + + /** + * Hides the keyboard. + * @param view The {@link View} that is currently accepting input. + * @return Whether the keyboard was visible before. + */ + private static boolean hideKeyboard(View view) { + InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService( + Context.INPUT_METHOD_SERVICE); + return imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } private static String getUrlFromIntent(Intent intent) { return intent != null ? intent.getDataString() : null; } - } -- 2.11.4.GIT