Chromium Code Reviews| 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.content.Context; | 8 import android.content.Context; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.content.pm.PackageInfo; | |
| 11 import android.content.pm.PackageManager; | |
| 10 import android.os.Bundle; | 12 import android.os.Bundle; |
| 13 | |
| 11 import android.view.KeyEvent; | 14 import android.view.KeyEvent; |
| 15 import android.view.MenuItem; | |
| 12 import android.view.View; | 16 import android.view.View; |
| 13 import android.view.View.OnKeyListener; | 17 import android.view.View.OnKeyListener; |
| 14 import android.view.inputmethod.InputMethodManager; | 18 import android.view.inputmethod.InputMethodManager; |
| 19 | |
| 15 import android.webkit.GeolocationPermissions; | 20 import android.webkit.GeolocationPermissions; |
| 16 import android.webkit.WebChromeClient; | 21 import android.webkit.WebChromeClient; |
| 22 import android.webkit.WebSettings; | |
| 17 import android.webkit.WebView; | 23 import android.webkit.WebView; |
| 18 import android.webkit.WebViewClient; | 24 import android.webkit.WebViewClient; |
| 25 | |
| 19 import android.widget.EditText; | 26 import android.widget.EditText; |
| 27 import android.widget.PopupMenu; | |
| 20 import android.widget.TextView; | 28 import android.widget.TextView; |
| 21 | 29 |
| 30 import org.chromium.base.Log; | |
| 31 | |
| 32 import java.lang.reflect.InvocationTargetException; | |
| 33 import java.lang.reflect.Method; | |
| 34 import java.text.DateFormat; | |
| 35 | |
| 22 /** | 36 /** |
| 23 * This activity is designed for starting a "mini-browser" for manual testing of WebView. | 37 * This activity is designed for starting a "mini-browser" for manual testing of WebView. |
| 24 * It takes an optional URL as an argument, and displays the page. There is a UR L bar | 38 * It takes an optional URL as an argument, and displays the page. There is a UR L bar |
| 25 * on top of the webview for manually specifying URLs to load. | 39 * on top of the webview for manually specifying URLs to load. |
| 26 */ | 40 */ |
| 27 public class WebViewBrowserActivity extends Activity { | 41 public class WebViewBrowserActivity extends Activity implements PopupMenu.OnMenu ItemClickListener { |
| 28 private EditText mUrlBar; | 42 private EditText mUrlBar; |
| 29 private WebView mWebView; | 43 private WebView mWebView; |
| 30 | 44 |
| 45 private static final String WEBVIEW_PACKAGE_NAME = "com.google.android.webvi ew"; | |
|
Torne
2015/05/06 11:01:39
This isn't the WebView package name on all configu
timvolodine
2015/05/07 10:43:47
as we discussed I've removed the packageInfo relat
| |
| 46 private static final String ABOUT_HEADER = "<html><body>"; | |
| 47 private static final String ABOUT_FOOTER = "</body></html>"; | |
| 48 | |
| 31 @Override | 49 @Override |
| 32 public void onCreate(Bundle savedInstanceState) { | 50 public void onCreate(Bundle savedInstanceState) { |
| 33 super.onCreate(savedInstanceState); | 51 super.onCreate(savedInstanceState); |
| 34 getWindow().setTitle( | 52 getWindow().setTitle(getResources().getString(R.string.title_activity_br owser)); |
| 35 getResources().getString(R.string.title_activity_browser)); | |
| 36 setContentView(R.layout.activity_webview_browser); | 53 setContentView(R.layout.activity_webview_browser); |
| 37 mWebView = (WebView) findViewById(R.id.webview); | 54 mWebView = (WebView) findViewById(R.id.webview); |
| 38 mWebView.getSettings().setJavaScriptEnabled(true); | 55 mWebView.getSettings().setJavaScriptEnabled(true); |
| 39 mWebView.getSettings().setGeolocationEnabled(true); | 56 mWebView.getSettings().setGeolocationEnabled(true); |
| 40 | 57 |
| 41 mWebView.setWebViewClient(new WebViewClient() { | 58 mWebView.setWebViewClient(new WebViewClient() { |
| 42 @Override | 59 @Override |
| 43 public boolean shouldOverrideUrlLoading(WebView webView, String url) { | 60 public boolean shouldOverrideUrlLoading(WebView webView, String url) { |
| 44 return false; | 61 return false; |
| 45 } | 62 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 69 mUrlBar.setText(url, TextView.BufferType.EDITABLE); | 86 mUrlBar.setText(url, TextView.BufferType.EDITABLE); |
| 70 loadUrl(url); | 87 loadUrl(url); |
| 71 } | 88 } |
| 72 } | 89 } |
| 73 | 90 |
| 74 public void loadUrlFromUrlBar(View view) { | 91 public void loadUrlFromUrlBar(View view) { |
| 75 loadUrl(mUrlBar.getText().toString()); | 92 loadUrl(mUrlBar.getText().toString()); |
| 76 hideKeyboard(mUrlBar); | 93 hideKeyboard(mUrlBar); |
| 77 } | 94 } |
| 78 | 95 |
| 96 public void showPopup(View v) { | |
| 97 PopupMenu popup = new PopupMenu(this, v); | |
| 98 popup.setOnMenuItemClickListener(this); | |
| 99 popup.inflate(R.menu.main_menu); | |
| 100 popup.show(); | |
| 101 } | |
| 102 | |
| 103 @Override | |
| 104 public boolean onMenuItemClick(MenuItem item) { | |
| 105 switch(item.getItemId()) { | |
| 106 case R.id.menu_about: | |
| 107 about(); | |
| 108 hideKeyboard(mUrlBar); | |
| 109 return true; | |
| 110 default: | |
| 111 return false; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 private void about() { | |
|
Torne
2015/05/06 11:01:39
I'd probably prefer this be a regular text popup d
timvolodine
2015/05/07 10:43:47
good point ;). I've added an AlertDialog instead
| |
| 116 WebSettings settings = mWebView.getSettings(); | |
| 117 PackageManager pm = getPackageManager(); | |
| 118 | |
| 119 StringBuilder summary = new StringBuilder(); | |
| 120 summary.append(ABOUT_HEADER); | |
| 121 try { | |
| 122 PackageInfo pi = pm.getPackageInfo(WEBVIEW_PACKAGE_NAME, 0); | |
| 123 summary.append("webview version : " + pi.versionName + ", " + pi.ver sionCode + "<br/>"); | |
| 124 summary.append("first install time : " | |
| 125 + DateFormat.getDateTimeInstance().format(pi.firstInstallTim e) + "<br/>"); | |
| 126 summary.append("last update time : " | |
| 127 + DateFormat.getDateTimeInstance().format(pi.lastUpdateTime) + "<br/><hr/>"); | |
| 128 } catch (PackageManager.NameNotFoundException e) { | |
| 129 Log.i("WebView", "Android System WebView not found"); | |
| 130 } | |
| 131 | |
| 132 // extract all no-argument methods returning a boolean or a String. | |
| 133 for (Method method : settings.getClass().getMethods()) { | |
| 134 if (!methodIsSimpleInspector(method)) continue; | |
| 135 try { | |
| 136 summary.append(method.getName() + " : " + method.invoke(settings ) + "<br/>"); | |
| 137 } catch (IllegalAccessException e) { | |
| 138 } catch (InvocationTargetException e) { } | |
| 139 } | |
| 140 | |
| 141 summary.append(ABOUT_FOOTER); | |
| 142 mWebView.loadData(summary.toString(), "text/html", null); | |
| 143 } | |
| 144 | |
| 145 // Returns true is a method has no arguments and returns either a boolean or a String. | |
| 146 private boolean methodIsSimpleInspector(Method method) { | |
| 147 Class<?> returnType = method.getReturnType(); | |
| 148 return ((returnType.equals(boolean.class) || returnType.equals(String.cl ass)) | |
| 149 && method.getParameterTypes().length == 0); | |
| 150 } | |
| 151 | |
| 79 private void loadUrl(String url) { | 152 private void loadUrl(String url) { |
| 80 mWebView.loadUrl(url); | 153 mWebView.loadUrl(url); |
| 81 mWebView.requestFocus(); | 154 mWebView.requestFocus(); |
| 82 } | 155 } |
| 83 | 156 |
| 84 /** | 157 /** |
| 85 * Hides the keyboard. | 158 * Hides the keyboard. |
| 86 * @param view The {@link View} that is currently accepting input. | 159 * @param view The {@link View} that is currently accepting input. |
| 87 * @return Whether the keyboard was visible before. | 160 * @return Whether the keyboard was visible before. |
| 88 */ | 161 */ |
| 89 private static boolean hideKeyboard(View view) { | 162 private static boolean hideKeyboard(View view) { |
| 90 InputMethodManager imm = (InputMethodManager) view.getContext().getSyste mService( | 163 InputMethodManager imm = (InputMethodManager) view.getContext().getSyste mService( |
| 91 Context.INPUT_METHOD_SERVICE); | 164 Context.INPUT_METHOD_SERVICE); |
| 92 return imm.hideSoftInputFromWindow(view.getWindowToken(), 0); | 165 return imm.hideSoftInputFromWindow(view.getWindowToken(), 0); |
| 93 } | 166 } |
| 94 | 167 |
| 95 private static String getUrlFromIntent(Intent intent) { | 168 private static String getUrlFromIntent(Intent intent) { |
| 96 return intent != null ? intent.getDataString() : null; | 169 return intent != null ? intent.getDataString() : null; |
| 97 } | 170 } |
| 98 } | 171 } |
| OLD | NEW |