blob: 2da0424dd0009dc8a71800ee22f56929fcd3cf03 [file] [log] [blame]
Michael Kolb11d19782011-03-20 10:17:40 -07001/*
John Reck0f602f32011-07-07 15:38:43 -07002 * Copyright (C) 2011 The Android Open Source Project
Michael Kolb11d19782011-03-20 10:17:40 -07003 *
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 */
Michael Kolb11d19782011-03-20 10:17:40 -070016package com.android.browser;
17
Michael Kolb11d19782011-03-20 10:17:40 -070018import android.app.Activity;
19import android.content.Context;
Michael Kolb2814a362011-05-19 15:49:41 -070020import android.content.res.Resources;
21import android.graphics.drawable.Drawable;
John Reck0f602f32011-07-07 15:38:43 -070022import android.util.AttributeSet;
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;
Michael Kolb017ffab2011-07-11 15:26:47 -070028import android.view.ViewConfiguration;
Michael Kolb2814a362011-05-19 15:49:41 -070029import android.webkit.WebView;
Michael Kolb11d19782011-03-20 10:17:40 -070030import android.widget.ImageView;
Michael Kolb017ffab2011-07-11 15:26:47 -070031import android.widget.PopupMenu;
32import android.widget.PopupMenu.OnDismissListener;
Michael Kolb11d19782011-03-20 10:17:40 -070033
Michael Kolb305b1c52011-06-21 16:16:22 -070034import com.android.browser.UrlInputView.StateListener;
Michael Kolb305b1c52011-06-21 16:16:22 -070035
John Reck0f602f32011-07-07 15:38:43 -070036public class NavigationBarPhone extends NavigationBarBase implements
37 StateListener, OnDismissListener {
Michael Kolb11d19782011-03-20 10:17:40 -070038
Michael Kolbc16c5952011-03-29 15:37:03 -070039 private ImageView mStopButton;
Michael Kolb11d19782011-03-20 10:17:40 -070040 private ImageView mVoiceButton;
Michael Kolb2814a362011-05-19 15:49:41 -070041 private Drawable mStopDrawable;
42 private Drawable mRefreshDrawable;
John Reck8ac42902011-06-29 16:14:34 -070043 private View mTabSwitcher;
Michael Kolb305b1c52011-06-21 16:16:22 -070044 private View mComboIcon;
45 private View mTitleContainer;
Michael Kolb017ffab2011-07-11 15:26:47 -070046 private View mMore;
Michael Kolb305b1c52011-06-21 16:16:22 -070047 private Drawable mTextfieldBgDrawable;
Michael Kolb8441d4b2011-07-18 14:50:21 -070048 private PopupMenu mPopupMenu;
Michael Kolb017ffab2011-07-11 15:26:47 -070049 private boolean mNeedsMenu;
Michael Kolb11d19782011-03-20 10:17:40 -070050
John Reck0f602f32011-07-07 15:38:43 -070051 public NavigationBarPhone(Context context) {
52 super(context);
53 }
54
55 public NavigationBarPhone(Context context, AttributeSet attrs) {
56 super(context, attrs);
57 }
58
59 public NavigationBarPhone(Context context, AttributeSet attrs, int defStyle) {
60 super(context, attrs, defStyle);
Michael Kolb11d19782011-03-20 10:17:40 -070061 }
62
63 @Override
John Reck0f602f32011-07-07 15:38:43 -070064 protected void onFinishInflate() {
65 super.onFinishInflate();
Michael Kolbc16c5952011-03-29 15:37:03 -070066 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070067 mStopButton.setOnClickListener(this);
68 mVoiceButton = (ImageView) findViewById(R.id.voice);
69 mVoiceButton.setOnClickListener(this);
John Reck8ac42902011-06-29 16:14:34 -070070 mTabSwitcher = findViewById(R.id.tab_switcher);
71 mTabSwitcher.setOnClickListener(this);
Michael Kolb017ffab2011-07-11 15:26:47 -070072 mMore = findViewById(R.id.more);
73 mMore.setOnClickListener(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070074 mComboIcon = findViewById(R.id.iconcombo);
75 mTitleContainer = findViewById(R.id.title_bg);
Michael Kolb11d19782011-03-20 10:17:40 -070076 setFocusState(false);
John Reck0f602f32011-07-07 15:38:43 -070077 Resources res = getContext().getResources();
Michael Kolb2814a362011-05-19 15:49:41 -070078 mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark);
79 mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolb305b1c52011-06-21 16:16:22 -070080 mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark);
81 setUaSwitcher(mComboIcon);
John Reck67f33632011-06-17 16:23:42 -070082 mUrlInput.setContainer(this);
Michael Kolb305b1c52011-06-21 16:16:22 -070083 mUrlInput.setStateListener(this);
John Reck0f602f32011-07-07 15:38:43 -070084 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
Michael Kolb11d19782011-03-20 10:17:40 -070085 }
86
87 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070088 public void createContextMenu(ContextMenu menu) {
John Reck0f602f32011-07-07 15:38:43 -070089 Activity activity = mBaseUi.getActivity();
90 MenuInflater inflater = activity.getMenuInflater();
Michael Kolb11d19782011-03-20 10:17:40 -070091 inflater.inflate(R.menu.title_context, menu);
John Reck0f602f32011-07-07 15:38:43 -070092 activity.onCreateContextMenu(menu, this, null);
Michael Kolb11d19782011-03-20 10:17:40 -070093 }
94
95 @Override
96 protected void setSearchMode(boolean voiceSearchEnabled) {
97 boolean showvoicebutton = voiceSearchEnabled &&
98 mUiController.supportsVoiceSearch();
99 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
100 View.GONE);
101 }
102
103 @Override
John Reck0f602f32011-07-07 15:38:43 -0700104 public void onProgressStarted() {
105 super.onProgressStarted();
106 if (mStopButton.getDrawable() != mStopDrawable) {
107 mStopButton.setImageDrawable(mStopDrawable);
108 if (mStopButton.getVisibility() != View.VISIBLE) {
109 mComboIcon.setVisibility(View.GONE);
110 mStopButton.setVisibility(View.VISIBLE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700111 }
John Reck0f602f32011-07-07 15:38:43 -0700112 }
113 }
114
115 @Override
116 public void onProgressStopped() {
117 super.onProgressStopped();
118 mStopButton.setVisibility(View.GONE);
119 mStopButton.setImageDrawable(mRefreshDrawable);
120 if (!isEditingUrl()) {
121 mComboIcon.setVisibility(View.VISIBLE);
Michael Kolb2814a362011-05-19 15:49:41 -0700122 }
Michael Kolb2814a362011-05-19 15:49:41 -0700123 }
124
Michael Kolb11d19782011-03-20 10:17:40 -0700125 /**
126 * Update the text displayed in the title bar.
127 * @param title String to display. If null, the new tab string will be
128 * shown.
129 */
130 @Override
131 void setDisplayTitle(String title) {
John Reck67f33632011-06-17 16:23:42 -0700132 if (!isEditingUrl()) {
133 if (title == null) {
134 mUrlInput.setText(R.string.new_tab);
135 } else {
136 mUrlInput.setText(title);
137 }
138 mUrlInput.setSelection(0);
Michael Kolb11d19782011-03-20 10:17:40 -0700139 }
140 }
141
142 @Override
Michael Kolb11d19782011-03-20 10:17:40 -0700143 public void onClick(View v) {
144 if (v == mStopButton) {
John Reck0f602f32011-07-07 15:38:43 -0700145 if (mTitleBar.isInLoad()) {
Michael Kolb2814a362011-05-19 15:49:41 -0700146 mUiController.stopLoading();
147 } else {
148 WebView web = mBaseUi.getWebView();
149 if (web != null) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700150 stopEditingUrl();
Michael Kolb2814a362011-05-19 15:49:41 -0700151 web.reload();
152 }
153 }
Michael Kolb11d19782011-03-20 10:17:40 -0700154 } else if (v == mVoiceButton) {
155 mUiController.startVoiceSearch();
John Reck8ac42902011-06-29 16:14:34 -0700156 } else if (v == mTabSwitcher) {
157 mBaseUi.onMenuKey();
Michael Kolb017ffab2011-07-11 15:26:47 -0700158 } else if (mMore == v) {
John Reckef654f12011-07-12 16:42:08 -0700159 showMenu(mMore);
Michael Kolb11d19782011-03-20 10:17:40 -0700160 } else {
161 super.onClick(v);
162 }
163 }
164
Michael Kolb017ffab2011-07-11 15:26:47 -0700165 public boolean isMenuShowing() {
Michael Kolb8441d4b2011-07-18 14:50:21 -0700166 return (mPopupMenu != null);
167 }
168
169 void showMenu() {
170 // called from menu key, use tab switcher as anchor
171 showMenu(mTabSwitcher);
Michael Kolb017ffab2011-07-11 15:26:47 -0700172 }
173
John Reckef654f12011-07-12 16:42:08 -0700174 void showMenu(View anchor) {
Michael Kolb8441d4b2011-07-18 14:50:21 -0700175 mPopupMenu = new PopupMenu(mContext, anchor);
176 Menu menu = mPopupMenu.getMenu();
177 mPopupMenu.getMenuInflater().inflate(R.menu.browser, menu);
Michael Kolb4bf79712011-07-14 14:18:12 -0700178 mUiController.updateMenuState(mBaseUi.getActiveTab(), menu);
Michael Kolb8441d4b2011-07-18 14:50:21 -0700179 mPopupMenu.setOnMenuItemClickListener(this);
180 mPopupMenu.setOnDismissListener(this);
181 mPopupMenu.show();
182 }
183
184 void dismissMenu() {
185 if (mPopupMenu != null) {
186 mPopupMenu.dismiss();
187 }
Michael Kolb017ffab2011-07-11 15:26:47 -0700188 }
189
190 @Override
191 public void onDismiss(PopupMenu menu) {
192 onMenuHidden();
193 }
194
195 @Override
196 public boolean onMenuItemClick(MenuItem item) {
197 onMenuHidden();
198 boolean res = mUiController.onOptionsItemSelected(item);
199 if (!res) {
200 return super.onMenuItemClick(item);
201 }
202 return res;
203 }
204
205 private void onMenuHidden() {
Michael Kolb8441d4b2011-07-18 14:50:21 -0700206 mPopupMenu = null;
Michael Kolb017ffab2011-07-11 15:26:47 -0700207 mBaseUi.showTitleBarForDuration();
208 }
209
Michael Kolb305b1c52011-06-21 16:16:22 -0700210 @Override
211 public void onStateChanged(int state) {
212 switch(state) {
213 case StateListener.STATE_NORMAL:
214 mComboIcon.setVisibility(View.VISIBLE);
215 mStopButton.setVisibility(View.GONE);
216 setSearchMode(false);
217 mTabSwitcher.setVisibility(View.VISIBLE);
218 mTitleContainer.setBackgroundDrawable(null);
Michael Kolb017ffab2011-07-11 15:26:47 -0700219 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700220 break;
221 case StateListener.STATE_HIGHLIGHTED:
222 mComboIcon.setVisibility(View.GONE);
223 mStopButton.setVisibility(View.VISIBLE);
224 setSearchMode(true);
225 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700226 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700227 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
228 break;
229 case StateListener.STATE_EDITED:
230 mComboIcon.setVisibility(View.GONE);
231 mStopButton.setVisibility(View.GONE);
232 setSearchMode(false);
233 mTabSwitcher.setVisibility(View.GONE);
Michael Kolb017ffab2011-07-11 15:26:47 -0700234 mMore.setVisibility(View.GONE);
Michael Kolb305b1c52011-06-21 16:16:22 -0700235 mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable);
236 break;
Michael Kolb2814a362011-05-19 15:49:41 -0700237 }
238 }
Michael Kolb11d19782011-03-20 10:17:40 -0700239}