Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / android / shell / java / src / org / chromium / chrome / shell / ChromeShellToolbar.java
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.
4
5 package org.chromium.chrome.shell;
6
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;
20
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;
26
27 /**
28  * A Toolbar {@link View} that shows the URL and navigation buttons.
29  */
30 public class ChromeShellToolbar extends LinearLayout {
31     private static final long COMPLETED_PROGRESS_TIMEOUT_MS = 200;
32
33     private final Runnable mClearProgressRunnable = new Runnable() {
34         @Override
35         public void run() {
36             mProgressDrawable.setLevel(0);
37         }
38     };
39
40     private EditText mUrlTextView;
41     private ClipDrawable mProgressDrawable;
42
43     private ChromeShellTab mTab;
44     private final TabObserver mTabObserver = new TabObserverImpl();
45
46     private AppMenuHandler mMenuHandler;
47     private AppMenuButtonHelper mAppMenuButtonHelper;
48
49     /**
50      * @param context The Context the view is running in.
51      * @param attrs   The attributes of the XML tag that is inflating the view.
52      */
53     public ChromeShellToolbar(Context context, AttributeSet attrs) {
54         super(context, attrs);
55     }
56
57     /**
58      * The toolbar will visually represent the state of {@code tab}.
59      * @param tab The Tab that should be represented.
60      */
61     public void showTab(ChromeShellTab tab) {
62         if (mTab != null) mTab.removeObserver(mTabObserver);
63         mTab = tab;
64         mTab.addObserver(mTabObserver);
65         mUrlTextView.setText(mTab.getContentViewCore().getUrl());
66     }
67
68     private void onUpdateUrl(String url) {
69         mUrlTextView.setText(url);
70     }
71
72     private void onLoadProgressChanged(int progress) {
73         removeCallbacks(mClearProgressRunnable);
74         mProgressDrawable.setLevel(100 * progress);
75         if (progress == 100) postDelayed(mClearProgressRunnable, COMPLETED_PROGRESS_TIMEOUT_MS);
76     }
77
78     @Override
79     protected void onFinishInflate() {
80         super.onFinishInflate();
81
82         mProgressDrawable = (ClipDrawable) findViewById(R.id.toolbar).getBackground();
83         initializeUrlField();
84         initializeMenuButton();
85     }
86
87     void setMenuHandler(AppMenuHandler menuHandler) {
88         mMenuHandler = menuHandler;
89         ImageButton menuButton = (ImageButton) findViewById(R.id.menu_button);
90         mAppMenuButtonHelper = new AppMenuButtonHelper(menuButton, mMenuHandler);
91     }
92
93     private void initializeUrlField() {
94         mUrlTextView = (EditText) findViewById(R.id.url);
95         mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() {
96             @Override
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)) {
101                     return false;
102                 }
103
104                 mTab.loadUrlWithSanitization(mUrlTextView.getText().toString());
105                 mUrlTextView.clearFocus();
106                 setKeyboardVisibilityForUrl(false);
107                 mTab.getContentView().requestFocus();
108                 return true;
109             }
110         });
111         mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
112             @Override
113             public void onFocusChange(View v, boolean hasFocus) {
114                 setKeyboardVisibilityForUrl(hasFocus);
115                 if (!hasFocus) {
116                     mUrlTextView.setText(mTab.getContentViewCore().getUrl());
117                 }
118             }
119         });
120     }
121
122     private void initializeMenuButton() {
123         ImageButton menuButton = (ImageButton) findViewById(R.id.menu_button);
124         menuButton.setOnClickListener(new OnClickListener() {
125             @Override
126             public void onClick(View view) {
127                 if (mMenuHandler != null) mMenuHandler.showAppMenu(view, false, false);
128             }
129         });
130         menuButton.setOnTouchListener(new OnTouchListener() {
131             @Override
132             public boolean onTouch(View view, MotionEvent event) {
133                 return mAppMenuButtonHelper != null && mAppMenuButtonHelper.onTouch(view, event);
134             }
135         });
136     }
137
138     private void setKeyboardVisibilityForUrl(boolean visible) {
139         InputMethodManager imm = (InputMethodManager) getContext().getSystemService(
140                 Context.INPUT_METHOD_SERVICE);
141         if (visible) {
142             imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
143         } else {
144             imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
145         }
146     }
147
148     private class TabObserverImpl extends EmptyTabObserver {
149         @Override
150         public void onLoadProgressChanged(Tab tab, int progress) {
151             if (tab == mTab) ChromeShellToolbar.this.onLoadProgressChanged(progress);
152         }
153
154         @Override
155         public void onUpdateUrl(Tab tab, String url) {
156             if (tab == mTab) ChromeShellToolbar.this.onUpdateUrl(url);
157         }
158     }
159 }