OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.webview_shell; | 5 package org.chromium.webview_shell; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.app.AlertDialog; |
8 import android.content.Context; | 9 import android.content.Context; |
9 import android.content.Intent; | 10 import android.content.Intent; |
10 import android.os.Bundle; | 11 import android.os.Bundle; |
| 12 |
11 import android.view.KeyEvent; | 13 import android.view.KeyEvent; |
| 14 import android.view.MenuItem; |
12 import android.view.View; | 15 import android.view.View; |
13 import android.view.View.OnKeyListener; | 16 import android.view.View.OnKeyListener; |
14 import android.view.inputmethod.InputMethodManager; | 17 import android.view.inputmethod.InputMethodManager; |
| 18 |
15 import android.webkit.GeolocationPermissions; | 19 import android.webkit.GeolocationPermissions; |
16 import android.webkit.PermissionRequest; | 20 import android.webkit.PermissionRequest; |
17 import android.webkit.WebChromeClient; | 21 import android.webkit.WebChromeClient; |
| 22 import android.webkit.WebSettings; |
18 import android.webkit.WebView; | 23 import android.webkit.WebView; |
19 import android.webkit.WebViewClient; | 24 import android.webkit.WebViewClient; |
| 25 |
20 import android.widget.EditText; | 26 import android.widget.EditText; |
| 27 import android.widget.LinearLayout.LayoutParams; |
| 28 import android.widget.PopupMenu; |
21 import android.widget.TextView; | 29 import android.widget.TextView; |
22 | 30 |
| 31 import java.lang.reflect.InvocationTargetException; |
| 32 import java.lang.reflect.Method; |
| 33 |
| 34 import java.util.regex.Matcher; |
| 35 import java.util.regex.Pattern; |
| 36 |
23 /** | 37 /** |
24 * This activity is designed for starting a "mini-browser" for manual testing of
WebView. | 38 * This activity is designed for starting a "mini-browser" for manual testing of
WebView. |
25 * It takes an optional URL as an argument, and displays the page. There is a UR
L bar | 39 * It takes an optional URL as an argument, and displays the page. There is a UR
L bar |
26 * on top of the webview for manually specifying URLs to load. | 40 * on top of the webview for manually specifying URLs to load. |
27 */ | 41 */ |
28 public class WebViewBrowserActivity extends Activity { | 42 public class WebViewBrowserActivity extends Activity implements PopupMenu.OnMenu
ItemClickListener { |
29 private EditText mUrlBar; | 43 private EditText mUrlBar; |
30 private WebView mWebView; | 44 private WebView mWebView; |
| 45 private String mWebViewVersion; |
| 46 |
| 47 private static final Pattern WEBVIEW_VERSION_PATTERN = |
| 48 Pattern.compile("(Chrome/)([\\d\\.]+)\\s"); |
31 | 49 |
32 private static final String[] AUTOMATICALLY_GRANT = | 50 private static final String[] AUTOMATICALLY_GRANT = |
33 { PermissionRequest.RESOURCE_VIDEO_CAPTURE, PermissionRequest.RESOUR
CE_AUDIO_CAPTURE }; | 51 { PermissionRequest.RESOURCE_VIDEO_CAPTURE, PermissionRequest.RESOUR
CE_AUDIO_CAPTURE }; |
34 | 52 |
35 @Override | 53 @Override |
36 public void onCreate(Bundle savedInstanceState) { | 54 public void onCreate(Bundle savedInstanceState) { |
37 super.onCreate(savedInstanceState); | 55 super.onCreate(savedInstanceState); |
38 getWindow().setTitle( | |
39 getResources().getString(R.string.title_activity_browser)); | |
40 setContentView(R.layout.activity_webview_browser); | 56 setContentView(R.layout.activity_webview_browser); |
41 mWebView = (WebView) findViewById(R.id.webview); | 57 mWebView = (WebView) findViewById(R.id.webview); |
42 mWebView.getSettings().setJavaScriptEnabled(true); | 58 WebSettings settings = mWebView.getSettings(); |
43 mWebView.getSettings().setGeolocationEnabled(true); | 59 initializeSettings(settings); |
| 60 |
| 61 Matcher matcher = WEBVIEW_VERSION_PATTERN.matcher(settings.getUserAgentS
tring()); |
| 62 if (matcher.find()) { |
| 63 mWebViewVersion = matcher.group(2); |
| 64 } else { |
| 65 mWebViewVersion = "-"; |
| 66 } |
| 67 setTitle(getResources().getString(R.string.title_activity_browser) + " "
+ mWebViewVersion); |
44 | 68 |
45 mWebView.setWebViewClient(new WebViewClient() { | 69 mWebView.setWebViewClient(new WebViewClient() { |
46 @Override | 70 @Override |
47 public boolean shouldOverrideUrlLoading(WebView webView, String url)
{ | 71 public boolean shouldOverrideUrlLoading(WebView webView, String url)
{ |
48 return false; | 72 return false; |
49 } | 73 } |
50 }); | 74 }); |
51 | 75 |
52 mWebView.setWebChromeClient(new WebChromeClient() { | 76 mWebView.setWebChromeClient(new WebChromeClient() { |
53 @Override | 77 @Override |
(...skipping 24 matching lines...) Expand all Loading... |
78 mUrlBar.setText(url, TextView.BufferType.EDITABLE); | 102 mUrlBar.setText(url, TextView.BufferType.EDITABLE); |
79 loadUrl(url); | 103 loadUrl(url); |
80 } | 104 } |
81 } | 105 } |
82 | 106 |
83 public void loadUrlFromUrlBar(View view) { | 107 public void loadUrlFromUrlBar(View view) { |
84 loadUrl(mUrlBar.getText().toString()); | 108 loadUrl(mUrlBar.getText().toString()); |
85 hideKeyboard(mUrlBar); | 109 hideKeyboard(mUrlBar); |
86 } | 110 } |
87 | 111 |
| 112 public void showPopup(View v) { |
| 113 PopupMenu popup = new PopupMenu(this, v); |
| 114 popup.setOnMenuItemClickListener(this); |
| 115 popup.inflate(R.menu.main_menu); |
| 116 popup.show(); |
| 117 } |
| 118 |
| 119 @Override |
| 120 public boolean onMenuItemClick(MenuItem item) { |
| 121 switch(item.getItemId()) { |
| 122 case R.id.menu_about: |
| 123 about(); |
| 124 hideKeyboard(mUrlBar); |
| 125 return true; |
| 126 default: |
| 127 return false; |
| 128 } |
| 129 } |
| 130 |
| 131 private void initializeSettings(WebSettings settings) { |
| 132 settings.setJavaScriptEnabled(true); |
| 133 settings.setGeolocationEnabled(true); |
| 134 } |
| 135 |
| 136 private void about() { |
| 137 WebSettings settings = mWebView.getSettings(); |
| 138 StringBuilder summary = new StringBuilder(); |
| 139 summary.append("WebView version : " + mWebViewVersion + "\n"); |
| 140 |
| 141 for (Method method : settings.getClass().getMethods()) { |
| 142 if (!methodIsSimpleInspector(method)) continue; |
| 143 try { |
| 144 summary.append(method.getName() + " : " + method.invoke(settings
) + "\n"); |
| 145 } catch (IllegalAccessException e) { |
| 146 } catch (InvocationTargetException e) { } |
| 147 } |
| 148 |
| 149 AlertDialog dialog = new AlertDialog.Builder(this) |
| 150 .setTitle(getResources().getString(R.string.menu_about)) |
| 151 .setMessage(summary) |
| 152 .setPositiveButton("OK", null) |
| 153 .create(); |
| 154 dialog.show(); |
| 155 dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL
_PARENT); |
| 156 } |
| 157 |
| 158 // Returns true is a method has no arguments and returns either a boolean or
a String. |
| 159 private boolean methodIsSimpleInspector(Method method) { |
| 160 Class<?> returnType = method.getReturnType(); |
| 161 return ((returnType.equals(boolean.class) || returnType.equals(String.cl
ass)) |
| 162 && method.getParameterTypes().length == 0); |
| 163 } |
| 164 |
88 private void loadUrl(String url) { | 165 private void loadUrl(String url) { |
89 mWebView.loadUrl(url); | 166 mWebView.loadUrl(url); |
90 mWebView.requestFocus(); | 167 mWebView.requestFocus(); |
91 } | 168 } |
92 | 169 |
93 /** | 170 /** |
94 * Hides the keyboard. | 171 * Hides the keyboard. |
95 * @param view The {@link View} that is currently accepting input. | 172 * @param view The {@link View} that is currently accepting input. |
96 * @return Whether the keyboard was visible before. | 173 * @return Whether the keyboard was visible before. |
97 */ | 174 */ |
98 private static boolean hideKeyboard(View view) { | 175 private static boolean hideKeyboard(View view) { |
99 InputMethodManager imm = (InputMethodManager) view.getContext().getSyste
mService( | 176 InputMethodManager imm = (InputMethodManager) view.getContext().getSyste
mService( |
100 Context.INPUT_METHOD_SERVICE); | 177 Context.INPUT_METHOD_SERVICE); |
101 return imm.hideSoftInputFromWindow(view.getWindowToken(), 0); | 178 return imm.hideSoftInputFromWindow(view.getWindowToken(), 0); |
102 } | 179 } |
103 | 180 |
104 private static String getUrlFromIntent(Intent intent) { | 181 private static String getUrlFromIntent(Intent intent) { |
105 return intent != null ? intent.getDataString() : null; | 182 return intent != null ? intent.getDataString() : null; |
106 } | 183 } |
107 } | 184 } |
OLD | NEW |