- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / android / testshell / java / src / org / chromium / chrome / testshell / TestShellToolbar.java
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.
4
5 package org.chromium.chrome.testshell;
6
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;
21
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;
26
27 /**
28  * A Toolbar {@link View} that shows the URL and navigation buttons.
29  */
30 public class TestShellToolbar extends LinearLayout {
31     private static final long COMPLETED_PROGRESS_TIMEOUT_MS = 200;
32
33     private Runnable mClearProgressRunnable = new Runnable() {
34         @Override
35         public void run() {
36             mProgressDrawable.setLevel(0);
37         }
38     };
39
40     private EditText mUrlTextView;
41     private ImageButton mPrevButton;
42     private ImageButton mNextButton;
43
44     private ClipDrawable mProgressDrawable;
45
46     private TestShellTab mTab;
47     private TabObserver mTabObserver = new TabObserverImpl();
48     private MenuHandler mMenuHandler;
49
50     /**
51      * @param context The Context the view is running in.
52      * @param attrs   The attributes of the XML tag that is inflating the view.
53      */
54     public TestShellToolbar(Context context, AttributeSet attrs) {
55         super(context, attrs);
56     }
57
58     /**
59      * The toolbar will visually represent the state of {@code tab}.
60      * @param tab The TabBase that should be represented.
61      */
62     public void showTab(TestShellTab tab) {
63         if (mTab != null) mTab.removeObserver(mTabObserver);
64         mTab = tab;
65         mTab.addObserver(mTabObserver);
66         mUrlTextView.setText(mTab.getContentView().getUrl());
67     }
68
69     private void onUpdateUrl(String url) {
70         mUrlTextView.setText(url);
71     }
72
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);
77     }
78
79     @Override
80     protected void onFinishInflate() {
81         super.onFinishInflate();
82
83         mProgressDrawable = (ClipDrawable) findViewById(R.id.toolbar).getBackground();
84         initializeUrlField();
85         initializeNavigationButtons();
86         initializeMenuButton();
87     }
88
89     public void setMenuHandler(MenuHandler menuHandler) {
90         mMenuHandler = menuHandler;
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.getKeyCode() != 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                 mNextButton.setVisibility(hasFocus ? GONE : VISIBLE);
116                 mPrevButton.setVisibility(hasFocus ? GONE : VISIBLE);
117                 if (!hasFocus) {
118                     mUrlTextView.setText(mTab.getContentView().getUrl());
119                 }
120             }
121         });
122     }
123
124     private void initializeNavigationButtons() {
125         mPrevButton = (ImageButton) findViewById(R.id.prev);
126         mPrevButton.setOnClickListener(new OnClickListener() {
127             @Override
128             public void onClick(View arg0) {
129                 if (mTab.canGoBack()) mTab.goBack();
130             }
131         });
132
133         mNextButton = (ImageButton) findViewById(R.id.next);
134         mNextButton.setOnClickListener(new OnClickListener() {
135             @Override
136             public void onClick(View v) {
137                 if (mTab.canGoForward()) mTab.goForward();
138             }
139         });
140     }
141
142     private void initializeMenuButton() {
143         ImageButton menuButton = (ImageButton) findViewById(R.id.menu_button);
144         menuButton.setOnClickListener(new OnClickListener() {
145             @Override
146             public void onClick(View v) {
147                 if (mMenuHandler != null) mMenuHandler.showPopupMenu();
148             }
149         });
150     }
151
152     private void setKeyboardVisibilityForUrl(boolean visible) {
153         InputMethodManager imm = (InputMethodManager) getContext().getSystemService(
154                 Context.INPUT_METHOD_SERVICE);
155         if (visible) {
156             imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT);
157         } else {
158             imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0);
159         }
160     }
161
162     private class TabObserverImpl extends EmptyTabObserver {
163         @Override
164         public void onLoadProgressChanged(TabBase tab, int progress) {
165             if (tab == mTab) TestShellToolbar.this.onLoadProgressChanged(progress);
166         }
167
168         @Override
169         public void onUpdateUrl(TabBase tab, String url) {
170             if (tab == mTab) TestShellToolbar.this.onUpdateUrl(url);
171         }
172     }
173 }