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.content.browser.LoadUrlParams; |
23 | |
24 | /** |
25 | * A Toolbar {@link View} that shows the URL and navigation buttons. |
26 | */ |
27 | public class TestShellToolbar extends LinearLayout { |
28 | private static final long COMPLETED_PROGRESS_TIMEOUT_MS = 200; |
29 | |
30 | private Runnable mClearProgressRunnable = new Runnable() { |
31 | @Override |
32 | public void run() { |
33 | mProgressDrawable.setLevel(0); |
34 | } |
35 | }; |
36 | |
37 | private EditText mUrlTextView; |
38 | private ImageButton mPrevButton; |
39 | private ImageButton mNextButton; |
40 | |
41 | private ClipDrawable mProgressDrawable; |
42 | |
43 | private TestShellTab mTab; |
44 | private TestShellTabObserver mTabObserver = new TestShellTabObserverImpl(); |
45 | private MenuHandler mMenuHandler; |
46 | |
47 | /** |
48 | * @param context The Context the view is running in. |
49 | * @param attrs The attributes of the XML tag that is inflating the view. |
50 | */ |
51 | public TestShellToolbar(Context context, AttributeSet attrs) { |
52 | super(context, attrs); |
53 | } |
54 | |
55 | /** |
56 | * The toolbar will visually represent the state of {@code tab}. |
57 | * @param tab The TabBase that should be represented. |
58 | */ |
59 | public void showTab(TestShellTab tab) { |
60 | if (mTab != null) mTab.removeObserver(mTabObserver); |
61 | mTab = tab; |
62 | mTab.addObserver(mTabObserver); |
63 | mUrlTextView.setText(mTab.getContentView().getUrl()); |
64 | } |
65 | |
66 | private void onUpdateUrl(String url) { |
67 | mUrlTextView.setText(url); |
68 | } |
69 | |
70 | private void onLoadProgressChanged(int progress) { |
71 | removeCallbacks(mClearProgressRunnable); |
72 | mProgressDrawable.setLevel((int) (100.0 * progress)); |
73 | if (progress == 100) postDelayed(mClearProgressRunnable, COMPLETED_PROGRESS_TIMEOUT_MS); |
74 | } |
75 | |
76 | @Override |
77 | protected void onFinishInflate() { |
78 | super.onFinishInflate(); |
79 | |
80 | mProgressDrawable = (ClipDrawable) findViewById(R.id.toolbar).getBackground(); |
81 | initializeUrlField(); |
82 | initializeNavigationButtons(); |
83 | initializeMenuButton(); |
84 | } |
85 | |
86 | public void setMenuHandler(MenuHandler menuHandler) { |
87 | mMenuHandler = menuHandler; |
88 | } |
89 | |
90 | private void initializeUrlField() { |
91 | mUrlTextView = (EditText) findViewById(R.id.url); |
92 | mUrlTextView.setOnEditorActionListener(new OnEditorActionListener() { |
93 | @Override |
94 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
95 | if ((actionId != EditorInfo.IME_ACTION_GO) && (event == null || |
96 | event.getKeyCode() != KeyEvent.KEYCODE_ENTER || |
97 | event.getKeyCode() != KeyEvent.ACTION_DOWN)) { |
98 | return false; |
99 | } |
100 | |
101 | mTab.loadUrlWithSanitization(mUrlTextView.getText().toString()); |
102 | mUrlTextView.clearFocus(); |
103 | setKeyboardVisibilityForUrl(false); |
104 | mTab.getContentView().requestFocus(); |
105 | return true; |
106 | } |
107 | }); |
108 | mUrlTextView.setOnFocusChangeListener(new OnFocusChangeListener() { |
109 | @Override |
110 | public void onFocusChange(View v, boolean hasFocus) { |
111 | setKeyboardVisibilityForUrl(hasFocus); |
112 | mNextButton.setVisibility(hasFocus ? GONE : VISIBLE); |
113 | mPrevButton.setVisibility(hasFocus ? GONE : VISIBLE); |
114 | if (!hasFocus) { |
115 | mUrlTextView.setText(mTab.getContentView().getUrl()); |
116 | } |
117 | } |
118 | }); |
119 | } |
120 | |
121 | private void initializeNavigationButtons() { |
122 | mPrevButton = (ImageButton) findViewById(R.id.prev); |
123 | mPrevButton.setOnClickListener(new OnClickListener() { |
124 | @Override |
125 | public void onClick(View arg0) { |
126 | if (mTab.getContentView().canGoBack()) mTab.getContentView().goBack(); |
127 | } |
128 | }); |
129 | |
130 | mNextButton = (ImageButton) findViewById(R.id.next); |
131 | mNextButton.setOnClickListener(new OnClickListener() { |
132 | @Override |
133 | public void onClick(View v) { |
134 | if (mTab.getContentView().canGoForward()) mTab.getContentView().goForward(); |
135 | } |
136 | }); |
137 | } |
138 | |
139 | private void initializeMenuButton() { |
140 | ImageButton menuButton = (ImageButton) findViewById(R.id.menu_button); |
141 | menuButton.setOnClickListener(new OnClickListener() { |
142 | @Override |
143 | public void onClick(View v) { |
144 | if (mMenuHandler != null) mMenuHandler.showPopupMenu(); |
145 | } |
146 | }); |
147 | } |
148 | |
149 | private void setKeyboardVisibilityForUrl(boolean visible) { |
150 | InputMethodManager imm = (InputMethodManager) getContext().getSystemService( |
151 | Context.INPUT_METHOD_SERVICE); |
152 | if (visible) { |
153 | imm.showSoftInput(mUrlTextView, InputMethodManager.SHOW_IMPLICIT); |
154 | } else { |
155 | imm.hideSoftInputFromWindow(mUrlTextView.getWindowToken(), 0); |
156 | } |
157 | } |
158 | |
159 | private class TestShellTabObserverImpl implements TestShellTabObserver { |
160 | @Override |
161 | public void onLoadProgressChanged(TestShellTab tab, int progress) { |
162 | if (tab == mTab) TestShellToolbar.this.onLoadProgressChanged(progress); |
163 | } |
164 | |
165 | @Override |
166 | public void onUpdateUrl(TestShellTab tab, String url) { |
167 | if (tab == mTab) TestShellToolbar.this.onUpdateUrl(url); |
168 | } |
169 | |
170 | @Override |
171 | public void onCloseTab(TestShellTab tab) { |
172 | } |
173 | } |
174 | } |