1 | // Copyright (c) 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. |
4 | |
5 | package org.chromium.android_webview.shell; |
6 | |
7 | import android.app.Activity; |
8 | import android.content.Intent; |
9 | import android.content.Context; |
10 | import android.content.SharedPreferences; |
11 | import android.os.Bundle; |
12 | import android.text.TextUtils; |
13 | import android.util.Log; |
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.LayoutParams; |
19 | import android.view.WindowManager; |
20 | import android.view.inputmethod.EditorInfo; |
21 | import android.view.inputmethod.InputMethodManager; |
22 | import android.widget.EditText; |
23 | import android.widget.ImageButton; |
24 | import android.widget.LinearLayout; |
25 | import android.widget.TextView; |
26 | import android.widget.TextView.OnEditorActionListener; |
27 | |
28 | import org.chromium.android_webview.AwBrowserProcess; |
29 | import org.chromium.android_webview.AwBrowserContext; |
30 | import org.chromium.android_webview.AwContents; |
31 | import org.chromium.android_webview.AwContentsClient; |
32 | import org.chromium.android_webview.AwLayoutSizer; |
33 | import org.chromium.android_webview.test.AwTestContainerView; |
34 | import org.chromium.android_webview.test.NullContentsClient; |
35 | import org.chromium.content.browser.LoadUrlParams; |
36 | |
37 | /* |
38 | * This is a lightweight activity for tests that only require WebView functionality. |
39 | */ |
40 | public class AwShellActivity extends Activity { |
41 | private final static String PREFERENCES_NAME = "AwShellPrefs"; |
42 | private final static String INITIAL_URL = "about:blank"; |
43 | private AwBrowserContext mBrowserContext; |
44 | private AwTestContainerView mAwTestContainerView; |
45 | private EditText mUrlTextView; |
46 | private ImageButton mPrevButton; |
47 | private ImageButton mNextButton; |
48 | |
49 | @Override |
50 | public void onCreate(Bundle savedInstanceState) { |
51 | super.onCreate(savedInstanceState); |
52 | |
53 | setContentView(R.layout.testshell_activity); |
54 | |
55 | mAwTestContainerView = createAwTestContainerView(); |
56 | |
57 | LinearLayout contentContainer = (LinearLayout) findViewById(R.id.content_container); |
58 | mAwTestContainerView.setLayoutParams(new LinearLayout.LayoutParams( |
59 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f)); |
60 | contentContainer.addView(mAwTestContainerView); |
61 | mAwTestContainerView.requestFocus(); |
62 | |
63 | initializeUrlField(); |
64 | initializeNavigationButtons(); |
65 | |
66 | String startupUrl = getUrlFromIntent(getIntent()); |
67 | if (TextUtils.isEmpty(startupUrl)) { |
68 | startupUrl = INITIAL_URL; |
69 | } |
70 | |
71 | mAwTestContainerView.getAwContents().loadUrl(new LoadUrlParams(startupUrl)); |
72 | mUrlTextView.setText(startupUrl); |
73 | } |
74 | |
75 | private AwTestContainerView createAwTestContainerView() { |
76 | AwTestContainerView testContainerView = new AwTestContainerView(this); |
77 | AwContentsClient awContentsClient = new NullContentsClient() { |
78 | @Override |
79 | public void onPageStarted(String url) { |
80 | if (mUrlTextView != null) { |
81 | mUrlTextView.setText(url); |
82 | } |
83 | } |
84 | }; |
85 | |
86 | SharedPreferences sharedPreferences = |
87 | getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE); |
88 | if (mBrowserContext == null) { |
89 | mBrowserContext = new AwBrowserContext(sharedPreferences); |
90 | } |
91 | testContainerView.initialize(new AwContents(mBrowserContext, testContainerView, |
92 | testContainerView.getInternalAccessDelegate(), |
93 | awContentsClient, false, new AwLayoutSizer(), true)); |
94 | testContainerView.getAwContents().getSettings().setJavaScriptEnabled(true); |
95 | return testContainerView; |
96 | } |
97 | |
98 | private static String getUrlFromIntent(Intent intent) { |
99 | return intent != null ? intent.getDataString() : null; |
100 | } |
101 | |
102 | private void setKeyboardVisibilityForUrl(boolean visible) { |
103 | InputMethodManager imm = (InputMethodManager) getSystemService( |
104 | Context.INPUT_METHOD_SERVICE); |
105 | if (visible) { |
106 | imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT); |
107 | } else { |
108 | imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0); |
109 | } |
110 | } |
111 | |
112 | private void initializeUrlField() { |
113 | mUrlTextView = (EditText) findViewById(R.id.url); |
114 | mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() { |
115 | @Override |
116 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
117 | if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null || |
118 | event.getKeyCode() != KeyEvent.KEYCODE_ENTER || |
119 | event.getKeyCode() != KeyEvent.ACTION_DOWN)) { |
120 | return false; |
121 | } |
122 | |
123 | mAwTestContainerView.getAwContents().loadUrl( |
124 | new LoadUrlParams(mUrlTextView.getText().toString())); |
125 | mUrlTextView.clearFocus(); |
126 | setKeyboardVisibilityForUrl(false); |
127 | mAwTestContainerView.requestFocus(); |
128 | return true; |
129 | } |
130 | }); |
131 | mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() { |
132 | @Override |
133 | public void onFocusChange(View v, boolean hasFocus) { |
134 | setKeyboardVisibilityForUrl(hasFocus); |
135 | mNextButton.setVisibility(hasFocus ? View.GONE : View.VISIBLE); |
136 | mPrevButton.setVisibility(hasFocus ? View.GONE : View.VISIBLE); |
137 | if (!hasFocus) { |
138 | mUrlTextView.setText(mAwTestContainerView.getContentViewCore().getUrl()); |
139 | } |
140 | } |
141 | }); |
142 | } |
143 | |
144 | private void initializeNavigationButtons() { |
145 | mPrevButton = (ImageButton) findViewById(R.id.prev); |
146 | mPrevButton.setOnClickListener(new OnClickListener() { |
147 | @Override |
148 | public void onClick(View v) { |
149 | if (mAwTestContainerView.getContentViewCore().canGoBack()) { |
150 | mAwTestContainerView.getContentViewCore().goBack(); |
151 | } |
152 | } |
153 | }); |
154 | |
155 | mNextButton = (ImageButton) findViewById(R.id.next); |
156 | mNextButton.setOnClickListener(new OnClickListener() { |
157 | @Override |
158 | public void onClick(View v) { |
159 | if (mAwTestContainerView.getContentViewCore().canGoForward()) { |
160 | mAwTestContainerView.getContentViewCore().goForward(); |
161 | } |
162 | } |
163 | }); |
164 | } |
165 | } |