1 // Copyright (c) 2012 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.chrome.testshell;
7 import android.content.Context;
8 import android.graphics.drawable.ClipDrawable;
9 import android.text.TextUtils;
10 import android.util.AttributeSet;
11 import android.view.KeyEvent;
12 import android.view.View;
13 import android.view.View.OnClickListener;
14 import android.view.inputmethod.EditorInfo;
15 import android.view.inputmethod.InputMethodManager;
16 import android.widget.EditText;
17 import android.widget.ImageButton;
18 import android.widget.LinearLayout;
19 import android.widget.TextView;
20 import android.widget.TextView.OnEditorActionListener;
22 import org.chromium.chrome.browser.TabBase;
23 import org.chromium.chrome.browser.TabObserver;
24 import org.chromium.chrome.browser.EmptyTabObserver;
25 import org.chromium.content.browser.LoadUrlParams;
28 * A Toolbar {@link View} that shows the URL and navigation buttons.
30 public class TestShellToolbar extends LinearLayout {
31 private static final long COMPLETED_PROGRESS_TIMEOUT_MS = 200;
33 private Runnable mClearProgressRunnable = new Runnable() {
36 mProgressDrawable.setLevel(0);
40 private EditText mUrlTextView;
41 private ImageButton mPrevButton;
42 private ImageButton mNextButton;
44 private ClipDrawable mProgressDrawable;
46 private TestShellTab mTab;
47 private TabObserver mTabObserver = new TabObserverImpl();
48 private MenuHandler mMenuHandler;
51 * @param context The Context the view is running in.
52 * @param attrs The attributes of the XML tag that is inflating the view.
54 public TestShellToolbar(Context context, AttributeSet attrs) {
55 super(context, attrs);
59 * The toolbar will visually represent the state of {@code tab}.
60 * @param tab The TabBase that should be represented.
62 public void showTab(TestShellTab tab) {
63 if (mTab != null) mTab.removeObserver(mTabObserver);
65 mTab.addObserver(mTabObserver);
66 mUrlTextView.setText(mTab.getContentView().getUrl());
69 private void onUpdateUrl(String url) {
70 mUrlTextView.setText(url);
73 private void onLoadProgressChanged(int progress) {
74 removeCallbacks(mClearProgressRunnable);
75 mProgressDrawable.setLevel((int) (100.0 * progress));
76 if (progress == 100) postDelayed(mClearProgressRunnable, COMPLETED_PROGRESS_TIMEOUT_MS);
80 protected void onFinishInflate() {
81 super.onFinishInflate();
83 mProgressDrawable = (ClipDrawable) findViewById(R.id.toolbar).getBackground();
85 initializeNavigationButtons();
86 initializeMenuButton();
89 public void setMenuHandler(MenuHandler menuHandler) {
90 mMenuHandler = menuHandler;
93 private void initializeUrlField() {
94 mUrlTextView = (EditText) findViewById(R.id.url);
95 mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
97 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
98 if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null ||
99 event.getKeyCode() != KeyEvent.KEYCODE_ENTER ||
100 event.getKeyCode() != KeyEvent.ACTION_DOWN)) {
104 mTab.loadUrlWithSanitization(mUrlTextView.getText().toString());
105 mUrlTextView.clearFocus();
106 setKeyboardVisibilityForUrl(false);
107 mTab.getContentView().requestFocus();
111 mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
113 public void onFocusChange(View v, boolean hasFocus) {
114 setKeyboardVisibilityForUrl(hasFocus);
115 mNextButton.setVisibility(hasFocus ? GONE : VISIBLE);
116 mPrevButton.setVisibility(hasFocus ? GONE : VISIBLE);
118 mUrlTextView.setText(mTab.getContentView().getUrl());
124 private void initializeNavigationButtons() {
125 mPrevButton = (ImageButton) findViewById(R.id.prev);
126 mPrevButton.setOnClickListener(new OnClickListener() {
128 public void onClick(View arg0) {
129 if (mTab.canGoBack()) mTab.goBack();
133 mNextButton = (ImageButton) findViewById(R.id.next);
134 mNextButton.setOnClickListener(new OnClickListener() {
136 public void onClick(View v) {
137 if (mTab.canGoForward()) mTab.goForward();
142 private void initializeMenuButton() {
143 ImageButton menuButton = (ImageButton) findViewById(R.id.menu_button);
144 menuButton.setOnClickListener(new OnClickListener() {
146 public void onClick(View v) {
147 if (mMenuHandler != null) mMenuHandler.showPopupMenu();
152 private void setKeyboardVisibilityForUrl(boolean visible) {
153 InputMethodManager imm = (InputMethodManager) getContext().getSystemService(
154 Context.INPUT_METHOD_SERVICE);
156 imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
158 imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
162 private class TabObserverImpl extends EmptyTabObserver {
164 public void onLoadProgressChanged(TabBase tab, int progress) {
165 if (tab == mTab) TestShellToolbar.this.onLoadProgressChanged(progress);
169 public void onUpdateUrl(TabBase tab, String url) {
170 if (tab == mTab) TestShellToolbar.this.onUpdateUrl(url);