Commit 11fb72d7 authored by timvolodine's avatar timvolodine Committed by Commit bot

[Android WebView] Add "about" menu with information about WebView settings to WebViewShell.

Add "about" popup menu to the "mini-browser" WebViewBrowserActivity of WebViewShell.
Clicking on the menu item results in information about WebView and current webview
settings to be printed using reflection.

BUG=481508

Review URL: https://codereview.chromium.org/1128573002

Cr-Commit-Position: refs/heads/master@{#330937}
parent 699df7e4
...@@ -29,6 +29,12 @@ ...@@ -29,6 +29,12 @@
android:src="@drawable/breadcrumb_arrow_black" android:src="@drawable/breadcrumb_arrow_black"
android:contentDescription="load url" android:contentDescription="load url"
android:onClick="loadUrlFromUrlBar" /> android:onClick="loadUrlFromUrlBar" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/item_more_black"
android:contentDescription="about webview"
android:onClick="showPopup" />
</LinearLayout> </LinearLayout>
<WebView <WebView
android:id="@+id/webview" android:id="@+id/webview"
......
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_about"
android:title="@string/menu_about"/>
</menu>
\ No newline at end of file
...@@ -10,4 +10,5 @@ ...@@ -10,4 +10,5 @@
<string name="title_activity_jank">WebView Jank Tester</string> <string name="title_activity_jank">WebView Jank Tester</string>
<string name="title_activity_startup_time">WebView Startup Time Tester</string> <string name="title_activity_startup_time">WebView Startup Time Tester</string>
<string name="title_activity_browser">WebView Browser Tester</string> <string name="title_activity_browser">WebView Browser Tester</string>
<string name="menu_about">About WebView</string>
</resources> </resources>
...@@ -5,29 +5,47 @@ ...@@ -5,29 +5,47 @@
package org.chromium.webview_shell; package org.chromium.webview_shell;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.View.OnKeyListener; import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.webkit.GeolocationPermissions; import android.webkit.GeolocationPermissions;
import android.webkit.PermissionRequest; import android.webkit.PermissionRequest;
import android.webkit.WebChromeClient; import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupMenu;
import android.widget.TextView; import android.widget.TextView;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* This activity is designed for starting a "mini-browser" for manual testing of WebView. * 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 * 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. * on top of the webview for manually specifying URLs to load.
*/ */
public class WebViewBrowserActivity extends Activity { public class WebViewBrowserActivity extends Activity implements PopupMenu.OnMenuItemClickListener {
private EditText mUrlBar; private EditText mUrlBar;
private WebView mWebView; private WebView mWebView;
private String mWebViewVersion;
private static final Pattern WEBVIEW_VERSION_PATTERN =
Pattern.compile("(Chrome/)([\\d\\.]+)\\s");
private static final String[] AUTOMATICALLY_GRANT = private static final String[] AUTOMATICALLY_GRANT =
{ PermissionRequest.RESOURCE_VIDEO_CAPTURE, PermissionRequest.RESOURCE_AUDIO_CAPTURE }; { PermissionRequest.RESOURCE_VIDEO_CAPTURE, PermissionRequest.RESOURCE_AUDIO_CAPTURE };
...@@ -35,12 +53,18 @@ public class WebViewBrowserActivity extends Activity { ...@@ -35,12 +53,18 @@ public class WebViewBrowserActivity extends Activity {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getWindow().setTitle(
getResources().getString(R.string.title_activity_browser));
setContentView(R.layout.activity_webview_browser); setContentView(R.layout.activity_webview_browser);
mWebView = (WebView) findViewById(R.id.webview); mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true); WebSettings settings = mWebView.getSettings();
mWebView.getSettings().setGeolocationEnabled(true); initializeSettings(settings);
Matcher matcher = WEBVIEW_VERSION_PATTERN.matcher(settings.getUserAgentString());
if (matcher.find()) {
mWebViewVersion = matcher.group(2);
} else {
mWebViewVersion = "-";
}
setTitle(getResources().getString(R.string.title_activity_browser) + " " + mWebViewVersion);
mWebView.setWebViewClient(new WebViewClient() { mWebView.setWebViewClient(new WebViewClient() {
@Override @Override
...@@ -85,6 +109,59 @@ public class WebViewBrowserActivity extends Activity { ...@@ -85,6 +109,59 @@ public class WebViewBrowserActivity extends Activity {
hideKeyboard(mUrlBar); hideKeyboard(mUrlBar);
} }
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.main_menu);
popup.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_about:
about();
hideKeyboard(mUrlBar);
return true;
default:
return false;
}
}
private void initializeSettings(WebSettings settings) {
settings.setJavaScriptEnabled(true);
settings.setGeolocationEnabled(true);
}
private void about() {
WebSettings settings = mWebView.getSettings();
StringBuilder summary = new StringBuilder();
summary.append("WebView version : " + mWebViewVersion + "\n");
for (Method method : settings.getClass().getMethods()) {
if (!methodIsSimpleInspector(method)) continue;
try {
summary.append(method.getName() + " : " + method.invoke(settings) + "\n");
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) { }
}
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle(getResources().getString(R.string.menu_about))
.setMessage(summary)
.setPositiveButton("OK", null)
.create();
dialog.show();
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
// Returns true is a method has no arguments and returns either a boolean or a String.
private boolean methodIsSimpleInspector(Method method) {
Class<?> returnType = method.getReturnType();
return ((returnType.equals(boolean.class) || returnType.equals(String.class))
&& method.getParameterTypes().length == 0);
}
private void loadUrl(String url) { private void loadUrl(String url) {
mWebView.loadUrl(url); mWebView.loadUrl(url);
mWebView.requestFocus(); mWebView.requestFocus();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment