1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.android_webview.shell;
7 import android.app.Activity;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.SharedPreferences;
11 import android.os.Bundle;
12 import android.text.TextUtils;
13 import android.view.Gravity;
14 import android.view.KeyEvent;
15 import android.view.View;
16 import android.view.View.OnClickListener;
17 import android.view.View.OnFocusChangeListener;
18 import android.view.ViewGroup;
19 import android.view.ViewGroup.LayoutParams;
20 import android.view.WindowManager;
21 import android.view.inputmethod.EditorInfo;
22 import android.view.inputmethod.InputMethodManager;
23 import android.webkit.WebChromeClient;
24 import android.widget.EditText;
25 import android.widget.FrameLayout;
26 import android.widget.ImageButton;
27 import android.widget.LinearLayout;
28 import android.widget.TextView;
29 import android.widget.TextView.OnEditorActionListener;
31 import org.chromium.android_webview.AwBrowserContext;
32 import org.chromium.android_webview.AwBrowserProcess;
33 import org.chromium.android_webview.AwContents;
34 import org.chromium.android_webview.AwContentsClient;
35 import org.chromium.android_webview.AwDevToolsServer;
36 import org.chromium.android_webview.AwSettings;
37 import org.chromium.android_webview.test.AwTestContainerView;
38 import org.chromium.android_webview.test.NullContentsClient;
39 import org.chromium.content.browser.LoadUrlParams;
42 * This is a lightweight activity for tests that only require WebView functionality.
44 public class AwShellActivity extends Activity {
45 private static final String PREFERENCES_NAME = "AwShellPrefs";
46 private static final String INITIAL_URL = "about:blank";
47 private AwBrowserContext mBrowserContext;
48 private AwDevToolsServer mDevToolsServer;
49 private AwTestContainerView mAwTestContainerView;
50 private EditText mUrlTextView;
51 private ImageButton mPrevButton;
52 private ImageButton mNextButton;
55 public void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
58 setContentView(R.layout.testshell_activity);
60 mAwTestContainerView = createAwTestContainerView();
62 LinearLayout contentContainer = (LinearLayout) findViewById(R.id.content_container);
63 mAwTestContainerView.setLayoutParams(new LinearLayout.LayoutParams(
64 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));
65 contentContainer.addView(mAwTestContainerView);
66 mAwTestContainerView.requestFocus();
69 initializeNavigationButtons();
71 String startupUrl = getUrlFromIntent(getIntent());
72 if (TextUtils.isEmpty(startupUrl)) {
73 startupUrl = INITIAL_URL;
76 mAwTestContainerView.getAwContents().loadUrl(new LoadUrlParams(startupUrl));
77 AwContents.setShouldDownloadFavicons();
78 mUrlTextView.setText(startupUrl);
81 private AwTestContainerView createAwTestContainerView() {
82 AwBrowserProcess.start(this);
83 AwTestContainerView testContainerView = new AwTestContainerView(this);
84 AwContentsClient awContentsClient = new NullContentsClient() {
85 private View mCustomView;
88 public void onPageStarted(String url) {
89 if (mUrlTextView != null) {
90 mUrlTextView.setText(url);
95 public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
97 WindowManager.LayoutParams.FLAG_FULLSCREEN,
98 WindowManager.LayoutParams.FLAG_FULLSCREEN);
100 getWindow().addContentView(view,
101 new FrameLayout.LayoutParams(
102 ViewGroup.LayoutParams.MATCH_PARENT,
103 ViewGroup.LayoutParams.MATCH_PARENT,
109 public void onHideCustomView() {
110 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
111 FrameLayout decorView = (FrameLayout) getWindow().getDecorView();
112 decorView.removeView(mCustomView);
117 public boolean shouldOverrideKeyEvent(KeyEvent event) {
118 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
125 SharedPreferences sharedPreferences =
126 getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
127 if (mBrowserContext == null) {
128 mBrowserContext = new AwBrowserContext(sharedPreferences);
130 final AwSettings awSettings = new AwSettings(this /*context*/,
131 false /*isAccessFromFileURLsGrantedByDefault*/, true /*supportsLegacyQuirks*/);
132 testContainerView.initialize(new AwContents(mBrowserContext, testContainerView,
133 testContainerView.getInternalAccessDelegate(),
134 awContentsClient, awSettings));
135 testContainerView.getAwContents().getSettings().setJavaScriptEnabled(true);
136 if (mDevToolsServer == null) {
137 mDevToolsServer = new AwDevToolsServer();
138 mDevToolsServer.setRemoteDebuggingEnabled(true);
140 return testContainerView;
143 private static String getUrlFromIntent(Intent intent) {
144 return intent != null ? intent.getDataString() : null;
147 private void setKeyboardVisibilityForUrl(boolean visible) {
148 InputMethodManager imm = (InputMethodManager) getSystemService(
149 Context.INPUT_METHOD_SERVICE);
151 imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
153 imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
157 private void initializeUrlField() {
158 mUrlTextView = (EditText) findViewById(R.id.url);
159 mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
161 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
162 if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null ||
163 event.getKeyCode() != KeyEvent.KEYCODE_ENTER ||
164 event.getAction() != KeyEvent.ACTION_DOWN)) {
168 mAwTestContainerView.getAwContents().loadUrl(
169 new LoadUrlParams(mUrlTextView.getText().toString()));
170 mUrlTextView.clearFocus();
171 setKeyboardVisibilityForUrl(false);
172 mAwTestContainerView.requestFocus();
176 mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
178 public void onFocusChange(View v, boolean hasFocus) {
179 setKeyboardVisibilityForUrl(hasFocus);
180 mNextButton.setVisibility(hasFocus ? View.GONE : View.VISIBLE);
181 mPrevButton.setVisibility(hasFocus ? View.GONE : View.VISIBLE);
183 mUrlTextView.setText(mAwTestContainerView.getContentViewCore().getUrl());
189 private void initializeNavigationButtons() {
190 mPrevButton = (ImageButton) findViewById(R.id.prev);
191 mPrevButton.setOnClickListener(new OnClickListener() {
193 public void onClick(View v) {
194 if (mAwTestContainerView.getContentViewCore().canGoBack()) {
195 mAwTestContainerView.getContentViewCore().goBack();
200 mNextButton = (ImageButton) findViewById(R.id.next);
201 mNextButton.setOnClickListener(new OnClickListener() {
203 public void onClick(View v) {
204 if (mAwTestContainerView.getContentViewCore().canGoForward()) {
205 mAwTestContainerView.getContentViewCore().goForward();
212 public boolean onKeyUp(int keyCode, KeyEvent event) {
213 if (keyCode == KeyEvent.KEYCODE_BACK) {
214 if (mAwTestContainerView.getContentViewCore().canGoBack()) {
215 mAwTestContainerView.getContentViewCore().goBack();
220 return super.onKeyUp(keyCode, event);