Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.content.Context; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
| 22 | import android.graphics.drawable.Drawable; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 23 | import android.view.ContextMenu; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 24 | import android.view.Menu; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 25 | import android.view.MenuInflater; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 26 | import android.view.MenuItem; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.view.View.OnClickListener; |
| 29 | import android.view.View.OnFocusChangeListener; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 30 | import android.view.ViewConfiguration; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 31 | import android.webkit.WebView; |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 33 | import android.widget.ImageView; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 34 | import android.widget.PopupMenu; |
| 35 | import android.widget.PopupMenu.OnDismissListener; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 36 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 37 | import com.android.browser.UrlInputView.StateListener; |
| 38 | import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher; |
| 39 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 40 | import java.util.List; |
| 41 | |
| 42 | /** |
| 43 | * This class represents a title bar for a particular "tab" or "window" in the |
| 44 | * browser. |
| 45 | */ |
| 46 | public class TitleBarPhone extends TitleBarBase implements OnFocusChangeListener, |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 47 | OnClickListener, TextChangeWatcher, StateListener, OnDismissListener { |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 48 | |
| 49 | private Activity mActivity; |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame] | 50 | private ImageView mStopButton; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 51 | private ImageView mVoiceButton; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 52 | private Drawable mStopDrawable; |
| 53 | private Drawable mRefreshDrawable; |
John Reck | 8ac4290 | 2011-06-29 16:14:34 -0700 | [diff] [blame] | 54 | private View mTabSwitcher; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 55 | private View mComboIcon; |
| 56 | private View mTitleContainer; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 57 | private View mMore; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 58 | private Drawable mTextfieldBgDrawable; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 59 | private boolean mMenuShowing; |
| 60 | private boolean mNeedsMenu; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 61 | |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 62 | public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui, |
| 63 | FrameLayout parent) { |
| 64 | super(activity, controller, ui, parent); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 65 | mNeedsMenu = !ViewConfiguration.get(activity).hasPermanentMenuKey(); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 66 | 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 Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 73 | mLockIcon = (ImageView) findViewById(R.id.lock); |
| 74 | mFavicon = (ImageView) findViewById(R.id.favicon); |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame] | 75 | mStopButton = (ImageView) findViewById(R.id.stop); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 76 | mStopButton.setOnClickListener(this); |
| 77 | mVoiceButton = (ImageView) findViewById(R.id.voice); |
| 78 | mVoiceButton.setOnClickListener(this); |
John Reck | 8ac4290 | 2011-06-29 16:14:34 -0700 | [diff] [blame] | 79 | mTabSwitcher = findViewById(R.id.tab_switcher); |
| 80 | mTabSwitcher.setOnClickListener(this); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 81 | mMore = findViewById(R.id.more); |
| 82 | mMore.setOnClickListener(this); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 83 | mComboIcon = findViewById(R.id.iconcombo); |
| 84 | mTitleContainer = findViewById(R.id.title_bg); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 85 | setFocusState(false); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 86 | 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 Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 89 | mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark); |
| 90 | setUaSwitcher(mComboIcon); |
John Reck | 67f3363 | 2011-06-17 16:23:42 -0700 | [diff] [blame] | 91 | mUrlInput.setContainer(this); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 92 | mUrlInput.setStateListener(this); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 96 | 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 Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 116 | void setProgress(int progress) { |
| 117 | super.setProgress(progress); |
| 118 | if (progress == 100) { |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 119 | mStopButton.setVisibility(View.GONE); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 120 | mStopButton.setImageDrawable(mRefreshDrawable); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 121 | 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 Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 132 | } |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 135 | /** |
| 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 Reck | 67f3363 | 2011-06-17 16:23:42 -0700 | [diff] [blame] | 142 | 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 Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 149 | } |
| 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 Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 165 | if (mInLoad) { |
| 166 | mUiController.stopLoading(); |
| 167 | } else { |
| 168 | WebView web = mBaseUi.getWebView(); |
| 169 | if (web != null) { |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 170 | stopEditingUrl(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 171 | web.reload(); |
| 172 | } |
| 173 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 174 | } else if (v == mVoiceButton) { |
| 175 | mUiController.startVoiceSearch(); |
John Reck | 8ac4290 | 2011-06-29 16:14:34 -0700 | [diff] [blame] | 176 | } else if (v == mTabSwitcher) { |
| 177 | mBaseUi.onMenuKey(); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 178 | } else if (mMore == v) { |
| 179 | showMenu(); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 180 | } else { |
| 181 | super.onClick(v); |
| 182 | } |
| 183 | } |
| 184 | |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 185 | 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 Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 220 | @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 Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 229 | mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 230 | 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 Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 236 | mMore.setVisibility(View.GONE); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 237 | 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 Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame^] | 244 | mMore.setVisibility(View.GONE); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 245 | mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable); |
| 246 | break; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 250 | } |