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