1 // Copyright 2014 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.shell;
7 import android.content.Context;
8 import android.graphics.drawable.ClipDrawable;
9 import android.util.AttributeSet;
10 import android.view.KeyEvent;
11 import android.view.MotionEvent;
12 import android.view.View;
13 import android.view.inputmethod.EditorInfo;
14 import android.view.inputmethod.InputMethodManager;
15 import android.widget.EditText;
16 import android.widget.ImageButton;
17 import android.widget.LinearLayout;
18 import android.widget.TextView;
19 import android.widget.TextView.OnEditorActionListener;
21 import org.chromium.chrome.browser.EmptyTabObserver;
22 import org.chromium.chrome.browser.Tab;
23 import org.chromium.chrome.browser.TabObserver;
24 import org.chromium.chrome.browser.appmenu.AppMenuButtonHelper;
25 import org.chromium.chrome.browser.appmenu.AppMenuHandler;
28 * A Toolbar {@link View} that shows the URL and navigation buttons.
30 public class ChromeShellToolbar extends LinearLayout {
31 private static final long COMPLETED_PROGRESS_TIMEOUT_MS = 200;
33 private final Runnable mClearProgressRunnable = new Runnable() {
36 mProgressDrawable.setLevel(0);
40 private EditText mUrlTextView;
41 private ClipDrawable mProgressDrawable;
43 private ChromeShellTab mTab;
44 private final TabObserver mTabObserver = new TabObserverImpl();
46 private AppMenuHandler mMenuHandler;
47 private AppMenuButtonHelper mAppMenuButtonHelper;
50 * @param context The Context the view is running in.
51 * @param attrs The attributes of the XML tag that is inflating the view.
53 public ChromeShellToolbar(Context context, AttributeSet attrs) {
54 super(context, attrs);
58 * The toolbar will visually represent the state of {@code tab}.
59 * @param tab The Tab that should be represented.
61 public void showTab(ChromeShellTab tab) {
62 if (mTab != null) mTab.removeObserver(mTabObserver);
64 mTab.addObserver(mTabObserver);
65 mUrlTextView.setText(mTab.getContentViewCore().getUrl());
68 private void onUpdateUrl(String url) {
69 mUrlTextView.setText(url);
72 private void onLoadProgressChanged(int progress) {
73 removeCallbacks(mClearProgressRunnable);
74 mProgressDrawable.setLevel(100 * progress);
75 if (progress == 100) postDelayed(mClearProgressRunnable, COMPLETED_PROGRESS_TIMEOUT_MS);
79 protected void onFinishInflate() {
80 super.onFinishInflate();
82 mProgressDrawable = (ClipDrawable) findViewById(R.id.toolbar).getBackground();
84 initializeMenuButton();
87 void setMenuHandler(AppMenuHandler menuHandler) {
88 mMenuHandler = menuHandler;
89 ImageButton menuButton = (ImageButton) findViewById(R.id.menu_button);
90 mAppMenuButtonHelper = new AppMenuButtonHelper(menuButton, mMenuHandler);
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.getAction() != 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);
116 mUrlTextView.setText(mTab.getContentViewCore().getUrl());
122 private void initializeMenuButton() {
123 ImageButton menuButton = (ImageButton) findViewById(R.id.menu_button);
124 menuButton.setOnClickListener(new OnClickListener() {
126 public void onClick(View view) {
127 if (mMenuHandler != null) mMenuHandler.showAppMenu(view, false, false);
130 menuButton.setOnTouchListener(new OnTouchListener() {
132 public boolean onTouch(View view, MotionEvent event) {
133 return mAppMenuButtonHelper != null && mAppMenuButtonHelper.onTouch(view, event);
138 private void setKeyboardVisibilityForUrl(boolean visible) {
139 InputMethodManager imm = (InputMethodManager) getContext().getSystemService(
140 Context.INPUT_METHOD_SERVICE);
142 imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
144 imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
148 private class TabObserverImpl extends EmptyTabObserver {
150 public void onLoadProgressChanged(Tab tab, int progress) {
151 if (tab == mTab) ChromeShellToolbar.this.onLoadProgressChanged(progress);
155 public void onUpdateUrl(Tab tab, String url) {
156 if (tab == mTab) ChromeShellToolbar.this.onUpdateUrl(url);