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.os.Bundle; | 10 import android.os.Bundle; |
11 import android.view.KeyEvent; | 11 import android.view.KeyEvent; |
12 import android.view.View; | 12 import android.view.View; |
13 import android.view.View.OnKeyListener; | 13 import android.view.View.OnKeyListener; |
14 import android.view.inputmethod.InputMethodManager; | 14 import android.view.inputmethod.InputMethodManager; |
15 import android.webkit.GeolocationPermissions; | 15 import android.webkit.GeolocationPermissions; |
| 16 import android.webkit.PermissionRequest; |
16 import android.webkit.WebChromeClient; | 17 import android.webkit.WebChromeClient; |
17 import android.webkit.WebView; | 18 import android.webkit.WebView; |
18 import android.webkit.WebViewClient; | 19 import android.webkit.WebViewClient; |
19 import android.widget.EditText; | 20 import android.widget.EditText; |
20 import android.widget.TextView; | 21 import android.widget.TextView; |
21 | 22 |
22 /** | 23 /** |
23 * This activity is designed for starting a "mini-browser" for manual testing of
WebView. | 24 * 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 | 25 * 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. | 26 * on top of the webview for manually specifying URLs to load. |
26 */ | 27 */ |
27 public class WebViewBrowserActivity extends Activity { | 28 public class WebViewBrowserActivity extends Activity { |
28 private EditText mUrlBar; | 29 private EditText mUrlBar; |
29 private WebView mWebView; | 30 private WebView mWebView; |
30 | 31 |
| 32 private static final String[] AUTOMATICALLY_GRANT = |
| 33 { PermissionRequest.RESOURCE_VIDEO_CAPTURE, PermissionRequest.RESOUR
CE_AUDIO_CAPTURE }; |
| 34 |
31 @Override | 35 @Override |
32 public void onCreate(Bundle savedInstanceState) { | 36 public void onCreate(Bundle savedInstanceState) { |
33 super.onCreate(savedInstanceState); | 37 super.onCreate(savedInstanceState); |
34 getWindow().setTitle( | 38 getWindow().setTitle( |
35 getResources().getString(R.string.title_activity_browser)); | 39 getResources().getString(R.string.title_activity_browser)); |
36 setContentView(R.layout.activity_webview_browser); | 40 setContentView(R.layout.activity_webview_browser); |
37 mWebView = (WebView) findViewById(R.id.webview); | 41 mWebView = (WebView) findViewById(R.id.webview); |
38 mWebView.getSettings().setJavaScriptEnabled(true); | 42 mWebView.getSettings().setJavaScriptEnabled(true); |
39 mWebView.getSettings().setGeolocationEnabled(true); | 43 mWebView.getSettings().setGeolocationEnabled(true); |
40 | 44 |
41 mWebView.setWebViewClient(new WebViewClient() { | 45 mWebView.setWebViewClient(new WebViewClient() { |
42 @Override | 46 @Override |
43 public boolean shouldOverrideUrlLoading(WebView webView, String url)
{ | 47 public boolean shouldOverrideUrlLoading(WebView webView, String url)
{ |
44 return false; | 48 return false; |
45 } | 49 } |
46 }); | 50 }); |
47 | 51 |
48 mWebView.setWebChromeClient(new WebChromeClient() { | 52 mWebView.setWebChromeClient(new WebChromeClient() { |
49 @Override | 53 @Override |
50 public void onGeolocationPermissionsShowPrompt(String origin, | 54 public void onGeolocationPermissionsShowPrompt(String origin, |
51 GeolocationPermissions.Callback callback) { | 55 GeolocationPermissions.Callback callback) { |
52 callback.invoke(origin, true, false); | 56 callback.invoke(origin, true, false); |
53 } | 57 } |
| 58 |
| 59 @Override |
| 60 public void onPermissionRequest(PermissionRequest request) { |
| 61 request.grant(AUTOMATICALLY_GRANT); |
| 62 } |
54 }); | 63 }); |
55 | 64 |
56 mUrlBar = (EditText) findViewById(R.id.url_field); | 65 mUrlBar = (EditText) findViewById(R.id.url_field); |
57 mUrlBar.setOnKeyListener(new OnKeyListener() { | 66 mUrlBar.setOnKeyListener(new OnKeyListener() { |
58 public boolean onKey(View view, int keyCode, KeyEvent event) { | 67 public boolean onKey(View view, int keyCode, KeyEvent event) { |
59 if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == Ke
yEvent.ACTION_UP) { | 68 if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == Ke
yEvent.ACTION_UP) { |
60 loadUrlFromUrlBar(view); | 69 loadUrlFromUrlBar(view); |
61 return true; | 70 return true; |
62 } | 71 } |
63 return false; | 72 return false; |
(...skipping 25 matching lines...) Expand all Loading... |
89 private static boolean hideKeyboard(View view) { | 98 private static boolean hideKeyboard(View view) { |
90 InputMethodManager imm = (InputMethodManager) view.getContext().getSyste
mService( | 99 InputMethodManager imm = (InputMethodManager) view.getContext().getSyste
mService( |
91 Context.INPUT_METHOD_SERVICE); | 100 Context.INPUT_METHOD_SERVICE); |
92 return imm.hideSoftInputFromWindow(view.getWindowToken(), 0); | 101 return imm.hideSoftInputFromWindow(view.getWindowToken(), 0); |
93 } | 102 } |
94 | 103 |
95 private static String getUrlFromIntent(Intent intent) { | 104 private static String getUrlFromIntent(Intent intent) { |
96 return intent != null ? intent.getDataString() : null; | 105 return intent != null ? intent.getDataString() : null; |
97 } | 106 } |
98 } | 107 } |
OLD | NEW |