blob: 66b464849e3bf651df8e09ed24f760e75be4c956 [file] [log] [blame]
Michael Kolb11d19782011-03-20 10:17:40 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb11d19782011-03-20 10:17:40 -070019import android.app.Activity;
20import android.content.Context;
Michael Kolb2814a362011-05-19 15:49:41 -070021import android.content.res.Resources;
22import android.graphics.drawable.Drawable;
Michael Kolb11d19782011-03-20 10:17:40 -070023import android.view.ContextMenu;
Michael Kolb017ffab2011-07-11 15:26:47 -070024import android.view.Menu;
Michael Kolb11d19782011-03-20 10:17:40 -070025import android.view.MenuInflater;
Michael Kolb017ffab2011-07-11 15:26:47 -070026import android.view.MenuItem;
Michael Kolb11d19782011-03-20 10:17:40 -070027import android.view.View;
28import android.view.View.OnClickListener;
29import android.view.View.OnFocusChangeListener;
Michael Kolb017ffab2011-07-11 15:26:47 -070030import android.view.ViewConfiguration;
Michael Kolb2814a362011-05-19 15:49:41 -070031import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070032import android.widget.FrameLayout;
Michael Kolb11d19782011-03-20 10:17:40 -070033import android.widget.ImageView;
Michael Kolb017ffab2011-07-11 15:26:47 -070034import android.widget.PopupMenu;
35import android.widget.PopupMenu.OnDismissListener;
Michael Kolb11d19782011-03-20 10:17:40 -070036
Michael Kolb305b1c52011-06-21 16:16:22 -070037import com.android.browser.UrlInputView.StateListener;
38import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
39
Michael Kolb11d19782011-03-20 10:17:40 -070040import java.util.List;
41
42/**
43 * This class represents a title bar for a particular "tab" or "window" in the
44 * browser.
45 */
46public class TitleBarPhone extends TitleBarBase implements OnFocusChangeListener,
Michael Kolb017ffab2011-07-11 15:26:47 -070047 OnClickListener, TextChangeWatcher, StateListener, OnDismissListener {
Michael Kolb11d19782011-03-20 10:17:40 -070048
49 private Activity mActivity;
Michael Kolbc16c5952011-03-29 15:37:03 -070050 private ImageView mStopButton;
Michael Kolb11d19782011-03-20 10:17:40 -070051 private ImageView mVoiceButton;
Michael Kolb2814a362011-05-19 15:49:41 -070052 private Drawable mStopDrawable;
53 private Drawable mRefreshDrawable;
John Reck8ac42902011-06-29 16:14:34 -070054 private View mTabSwitcher;
Michael Kolb305b1c52011-06-21 16:16:22 -070055 private View mComboIcon;
56 private View mTitleContainer;
Michael Kolb017ffab2011-07-11 15:26:47 -070057 private View mMore;
Michael Kolb305b1c52011-06-21 16:16:22 -070058 private Drawable mTextfieldBgDrawable;
Michael Kolb017ffab2011-07-11 15:26:47 -070059 private boolean mMenuShowing;
60 private boolean mNeedsMenu;
Michael Kolb11d19782011-03-20 10:17:40 -070061
Michael Kolb46f987e2011-04-05 10:39:10 -070062 public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui,
63 FrameLayout parent) {
64 super(activity, controller, ui, parent);
Michael Kolb017ffab2011-07-11 15:26:47 -070065 mNeedsMenu = !ViewConfiguration.get(activity).hasPermanentMenuKey();
Michael Kolb11d19782011-03-20 10:17:40 -070066 mActivity = activity;
67 initLayout(activity, R.layout.title_bar);
68 }
69
70 @Override
71 protected void initLayout(Context context, int layoutId) {
72 super.initLayout(context, layoutId);
Michael Kolb11d19782011-03-20 10:17:40 -070073 mLockIcon = (ImageView) findViewById(R.id.lock);
74 mFavicon = (ImageView) findViewById(R.id.favicon);
Michael Kolbc16c5952011-03-29 15:37:03 -070075 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070076 mStopButton.setOnClickListener(this);
77 mVoiceButton = (ImageView) findViewById(R.id.voice);
78 mVoiceButton.setOnClickListener(this);
John Reck8ac42902011-06-29 16:14:34 -070079 mTabSwitcher = findViewById(R.id.tab_switcher);
80 mTabSwitcher.setOnClickListener(this);
Michael Kolb017ffab2011-07-11 15:26:47 -070081 mMore = findViewById(R.id.more);
82 mMore.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070083 mComboIcon = findViewById(R.id.iconcombo);
84 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070085 setFocusState(false);
Michael Kolb2814a362011-05-19 15:49:41 -070086 Resources res = context.getResources();
87 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
88 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb305b1c52011-06-21 16:16:22 -070089 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
90 setUaSwitcher(mComboIcon);
John Reck67f33632011-06-17 16:23:42 -070091 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070092 mUrlInput.setStateListener(this);
Michael Kolb11d19782011-03-20 10:17:40 -070093 }
94
95 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070096 public void createContextMenu(ContextMenu menu) {
97 MenuInflater inflater = mActivity.getMenuInflater();
98 inflater.inflate(R.menu.title_context, menu);
99 mActivity.onCreateContextMenu(menu, this, null);
100 }
101
102 @Override
103 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
104 super.setInVoiceMode(voicemode, voiceResults);
105 }
106
107 @Override
108 protected void setSearchMode(boolean voiceSearchEnabled) {
109 boolean showvoicebutton = voiceSearchEnabled &&
110 mUiController.supportsVoiceSearch();
111 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
112 View.GONE);
113 }
114
115 @Override
Michael Kolb2814a362011-05-19 15:49:41 -0700116 void setProgress(int progress) {
117 super.setProgress(progress);
118 if (progress == 100) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700119 mStopButton.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700120 mStopButton.setImageDrawable(mRefreshDrawable);
Michael Kolb305b1c52011-06-21 16:16:22 -0700121 if (!isEditingUrl()) {
122 mComboIcon.setVisibility(View.VISIBLE);
123 }
124 } else {
125 if (mStopButton.getDrawable() != mStopDrawable) {
126 mStopButton.setImageDrawable(mStopDrawable);
127 if (mStopButton.getVisibility() != View.VISIBLE) {
128 mComboIcon.setVisibility(View.GONE);
129 mStopButton.setVisibility(View.VISIBLE);
130 }
131 }
Michael Kolb2814a362011-05-19 15:49:41 -0700132 }
Michael Kolb2814a362011-05-19 15:49:41 -0700133 }
134
Michael Kolb11d19782011-03-20 10:17:40 -0700135 /**
136 * Update the text displayed in the title bar.
137 * @param title String to display. If null, the new tab string will be
138 * shown.
139 */
140 @Override
141 void setDisplayTitle(String title) {
John Reck67f33632011-06-17 16:23:42 -0700142 if (!isEditingUrl()) {
143 if (title == null) {
144 mUrlInput.setText(R.string.new_tab);
145 } else {
146 mUrlInput.setText(title);
147 }
148 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700149 }
150 }
151
152 @Override
153 public void onFocusChange(View v, boolean hasFocus) {
154 if (v == mUrlInput) {
155 if (hasFocus) {
156 mActivity.closeOptionsMenu();
157 }
158 }
159 super.onFocusChange(v, hasFocus);
160 }
161
162 @Override
163 public void onClick(View v) {
164 if (v == mStopButton) {
Michael Kolb2814a362011-05-19 15:49:41 -0700165 if (mInLoad) {
166 mUiController.stopLoading();
167 } else {
168 WebView web = mBaseUi.getWebView();
169 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700170 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700171 web.reload();
172 }
173 }
Michael Kolb11d19782011-03-20 10:17:40 -0700174 } else if (v == mVoiceButton) {
175 mUiController.startVoiceSearch();
John Reck8ac42902011-06-29 16:14:34 -0700176 } else if (v == mTabSwitcher) {
177 mBaseUi.onMenuKey();
Michael Kolb017ffab2011-07-11 15:26:47 -0700178 } else if (mMore == v) {
179 showMenu();
Michael Kolb11d19782011-03-20 10:17:40 -0700180 } else {
181 super.onClick(v);
182 }
183 }
184
Michael Kolb017ffab2011-07-11 15:26:47 -0700185 public boolean isMenuShowing() {
186 return mMenuShowing;
187 }
188
189 private void showMenu() {
190 mMenuShowing = true;
191 PopupMenu popup = new PopupMenu(mContext, mMore);
192 Menu menu = popup.getMenu();
193 popup.getMenuInflater().inflate(R.menu.browser, menu);
194 menu.setGroupVisible(R.id.NAV_MENU, false);
195 popup.setOnMenuItemClickListener(this);
196 popup.setOnDismissListener(this);
197 popup.show();
198 }
199
200 @Override
201 public void onDismiss(PopupMenu menu) {
202 onMenuHidden();
203 }
204
205 @Override
206 public boolean onMenuItemClick(MenuItem item) {
207 onMenuHidden();
208 boolean res = mUiController.onOptionsItemSelected(item);
209 if (!res) {
210 return super.onMenuItemClick(item);
211 }
212 return res;
213 }
214
215 private void onMenuHidden() {
216 mMenuShowing = false;
217 mBaseUi.showTitleBarForDuration();
218 }
219
Michael Kolb305b1c52011-06-21 16:16:22 -0700220 @Override
221 public void onStateChanged(int state) {
222 switch(state) {
223 case StateListener.STATE_NORMAL:
224 mComboIcon.setVisibility(View.VISIBLE);
225 mStopButton.setVisibility(View.GONE);
226 setSearchMode(false);
227 mTabSwitcher.setVisibility(View.VISIBLE);
228 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700229 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700230 break;
231 case StateListener.STATE_HIGHLIGHTED:
232 mComboIcon.setVisibility(View.GONE);
233 mStopButton.setVisibility(View.VISIBLE);
234 setSearchMode(true);
235 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700236 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700237 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
238 break;
239 case StateListener.STATE_EDITED:
240 mComboIcon.setVisibility(View.GONE);
241 mStopButton.setVisibility(View.GONE);
242 setSearchMode(false);
243 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700244 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700245 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
246 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700247 }
248 }
249
Michael Kolb11d19782011-03-20 10:17:40 -0700250}