Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 1 | /* |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011 The Android Open Source Project |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 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 | */ |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 16 | package com.android.browser; |
| 17 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 18 | import android.content.Context; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 19 | import android.content.res.Resources; |
| 20 | import android.graphics.drawable.Drawable; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 21 | import android.util.AttributeSet; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 22 | import android.view.Menu; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 23 | import android.view.MenuItem; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 24 | import android.view.View; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 25 | import android.view.ViewConfiguration; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 26 | import android.webkit.WebView; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 27 | import android.widget.ImageView; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 28 | import android.widget.PopupMenu; |
| 29 | import android.widget.PopupMenu.OnDismissListener; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 30 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 31 | import com.android.browser.UrlInputView.StateListener; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 32 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 33 | public class NavigationBarPhone extends NavigationBarBase implements |
John Reck | 5889190 | 2011-08-11 17:48:53 -0700 | [diff] [blame] | 34 | StateListener { |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 35 | |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame] | 36 | private ImageView mStopButton; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 37 | private ImageView mVoiceButton; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 38 | private Drawable mStopDrawable; |
| 39 | private Drawable mRefreshDrawable; |
Michael Kolb | 30adae6 | 2011-07-29 13:37:05 -0700 | [diff] [blame] | 40 | private String mStopDescription; |
| 41 | private String mRefreshDescription; |
John Reck | 8ac4290 | 2011-06-29 16:14:34 -0700 | [diff] [blame] | 42 | private View mTabSwitcher; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 43 | private View mComboIcon; |
| 44 | private View mTitleContainer; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 45 | private View mMore; |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 46 | private Drawable mTextfieldBgDrawable; |
Michael Kolb | 8441d4b | 2011-07-18 14:50:21 -0700 | [diff] [blame] | 47 | private PopupMenu mPopupMenu; |
John Reck | 5889190 | 2011-08-11 17:48:53 -0700 | [diff] [blame] | 48 | private boolean mOverflowMenuShowing; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 49 | private boolean mNeedsMenu; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 50 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 51 | 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 Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 64 | protected void onFinishInflate() { |
| 65 | super.onFinishInflate(); |
Michael Kolb | c16c595 | 2011-03-29 15:37:03 -0700 | [diff] [blame] | 66 | mStopButton = (ImageView) findViewById(R.id.stop); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 67 | mStopButton.setOnClickListener(this); |
| 68 | mVoiceButton = (ImageView) findViewById(R.id.voice); |
| 69 | mVoiceButton.setOnClickListener(this); |
John Reck | 8ac4290 | 2011-06-29 16:14:34 -0700 | [diff] [blame] | 70 | mTabSwitcher = findViewById(R.id.tab_switcher); |
| 71 | mTabSwitcher.setOnClickListener(this); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 72 | mMore = findViewById(R.id.more); |
| 73 | mMore.setOnClickListener(this); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 74 | mComboIcon = findViewById(R.id.iconcombo); |
| 75 | mTitleContainer = findViewById(R.id.title_bg); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 76 | setFocusState(false); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 77 | Resources res = getContext().getResources(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 78 | mStopDrawable = res.getDrawable(R.drawable.ic_stop_holo_dark); |
| 79 | mRefreshDrawable = res.getDrawable(R.drawable.ic_refresh_holo_dark); |
Michael Kolb | 30adae6 | 2011-07-29 13:37:05 -0700 | [diff] [blame] | 80 | mStopDescription = res.getString(R.string.accessibility_button_stop); |
| 81 | mRefreshDescription = res.getString(R.string.accessibility_button_refresh); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 82 | mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark); |
| 83 | setUaSwitcher(mComboIcon); |
John Reck | 67f3363 | 2011-06-17 16:23:42 -0700 | [diff] [blame] | 84 | mUrlInput.setContainer(this); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 85 | mUrlInput.setStateListener(this); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 86 | mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey(); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 90 | protected void setSearchMode(boolean voiceSearchEnabled) { |
| 91 | boolean showvoicebutton = voiceSearchEnabled && |
| 92 | mUiController.supportsVoiceSearch(); |
| 93 | mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE : |
| 94 | View.GONE); |
| 95 | } |
| 96 | |
| 97 | @Override |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 98 | public void onProgressStarted() { |
| 99 | super.onProgressStarted(); |
| 100 | if (mStopButton.getDrawable() != mStopDrawable) { |
| 101 | mStopButton.setImageDrawable(mStopDrawable); |
Michael Kolb | 30adae6 | 2011-07-29 13:37:05 -0700 | [diff] [blame] | 102 | mStopButton.setContentDescription(mStopDescription); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 103 | if (mStopButton.getVisibility() != View.VISIBLE) { |
| 104 | mComboIcon.setVisibility(View.GONE); |
| 105 | mStopButton.setVisibility(View.VISIBLE); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 106 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void onProgressStopped() { |
| 112 | super.onProgressStopped(); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 113 | mStopButton.setImageDrawable(mRefreshDrawable); |
Michael Kolb | 30adae6 | 2011-07-29 13:37:05 -0700 | [diff] [blame] | 114 | mStopButton.setContentDescription(mRefreshDescription); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 115 | if (!isEditingUrl()) { |
| 116 | mComboIcon.setVisibility(View.VISIBLE); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 117 | } |
Michael Kolb | 5e8f2b9 | 2011-07-19 13:22:32 -0700 | [diff] [blame] | 118 | onStateChanged(mUrlInput.getState()); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 121 | /** |
| 122 | * Update the text displayed in the title bar. |
| 123 | * @param title String to display. If null, the new tab string will be |
| 124 | * shown. |
| 125 | */ |
| 126 | @Override |
| 127 | void setDisplayTitle(String title) { |
John Reck | 434e9f8 | 2011-08-10 18:16:52 -0700 | [diff] [blame] | 128 | mUrlInput.setTag(title); |
John Reck | 67f3363 | 2011-06-17 16:23:42 -0700 | [diff] [blame] | 129 | if (!isEditingUrl()) { |
| 130 | if (title == null) { |
| 131 | mUrlInput.setText(R.string.new_tab); |
| 132 | } else { |
John Reck | 434e9f8 | 2011-08-10 18:16:52 -0700 | [diff] [blame] | 133 | mUrlInput.setText(UrlUtils.stripUrl(title), false); |
John Reck | 67f3363 | 2011-06-17 16:23:42 -0700 | [diff] [blame] | 134 | } |
| 135 | mUrlInput.setSelection(0); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 140 | public void onClick(View v) { |
| 141 | if (v == mStopButton) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 142 | if (mTitleBar.isInLoad()) { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 143 | mUiController.stopLoading(); |
| 144 | } else { |
| 145 | WebView web = mBaseUi.getWebView(); |
| 146 | if (web != null) { |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 147 | stopEditingUrl(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 148 | web.reload(); |
| 149 | } |
| 150 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 151 | } else if (v == mVoiceButton) { |
| 152 | mUiController.startVoiceSearch(); |
John Reck | 8ac4290 | 2011-06-29 16:14:34 -0700 | [diff] [blame] | 153 | } else if (v == mTabSwitcher) { |
Michael Kolb | 20be26d | 2011-07-18 16:38:02 -0700 | [diff] [blame] | 154 | ((PhoneUi) mBaseUi).toggleNavScreen(); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 155 | } else if (mMore == v) { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 156 | showMenu(mMore); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 157 | } else { |
| 158 | super.onClick(v); |
| 159 | } |
| 160 | } |
| 161 | |
John Reck | 5889190 | 2011-08-11 17:48:53 -0700 | [diff] [blame] | 162 | @Override |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 163 | public boolean isMenuShowing() { |
John Reck | 5889190 | 2011-08-11 17:48:53 -0700 | [diff] [blame] | 164 | return super.isMenuShowing() || mOverflowMenuShowing; |
Michael Kolb | 8441d4b | 2011-07-18 14:50:21 -0700 | [diff] [blame] | 165 | } |
| 166 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 167 | void showMenu(View anchor) { |
John Reck | 5889190 | 2011-08-11 17:48:53 -0700 | [diff] [blame] | 168 | mOverflowMenuShowing = true; |
Michael Kolb | 8441d4b | 2011-07-18 14:50:21 -0700 | [diff] [blame] | 169 | mPopupMenu = new PopupMenu(mContext, anchor); |
| 170 | Menu menu = mPopupMenu.getMenu(); |
| 171 | mPopupMenu.getMenuInflater().inflate(R.menu.browser, menu); |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 172 | mUiController.updateMenuState(mBaseUi.getActiveTab(), menu); |
Michael Kolb | 8441d4b | 2011-07-18 14:50:21 -0700 | [diff] [blame] | 173 | mPopupMenu.setOnMenuItemClickListener(this); |
| 174 | mPopupMenu.setOnDismissListener(this); |
| 175 | mPopupMenu.show(); |
| 176 | } |
| 177 | |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 178 | @Override |
| 179 | public void onDismiss(PopupMenu menu) { |
John Reck | 5889190 | 2011-08-11 17:48:53 -0700 | [diff] [blame] | 180 | if (menu == mPopupMenu) { |
| 181 | onMenuHidden(); |
| 182 | } |
| 183 | super.onDismiss(menu); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public boolean onMenuItemClick(MenuItem item) { |
| 188 | onMenuHidden(); |
| 189 | boolean res = mUiController.onOptionsItemSelected(item); |
| 190 | if (!res) { |
| 191 | return super.onMenuItemClick(item); |
| 192 | } |
| 193 | return res; |
| 194 | } |
| 195 | |
| 196 | private void onMenuHidden() { |
John Reck | 5889190 | 2011-08-11 17:48:53 -0700 | [diff] [blame] | 197 | mOverflowMenuShowing = false; |
Michael Kolb | 8441d4b | 2011-07-18 14:50:21 -0700 | [diff] [blame] | 198 | mPopupMenu = null; |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 199 | mBaseUi.showTitleBarForDuration(); |
| 200 | } |
| 201 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 202 | @Override |
John Reck | 434e9f8 | 2011-08-10 18:16:52 -0700 | [diff] [blame] | 203 | public void onFocusChange(View view, boolean hasFocus) { |
| 204 | if (view == mUrlInput) { |
| 205 | if (hasFocus) { |
| 206 | mUrlInput.setText((String) mUrlInput.getTag(), false); |
| 207 | } else { |
| 208 | setDisplayTitle(mUrlInput.getText().toString()); |
| 209 | } |
| 210 | } |
| 211 | super.onFocusChange(view, hasFocus); |
| 212 | } |
| 213 | |
| 214 | @Override |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 215 | public void onStateChanged(int state) { |
| 216 | switch(state) { |
| 217 | case StateListener.STATE_NORMAL: |
| 218 | mComboIcon.setVisibility(View.VISIBLE); |
| 219 | mStopButton.setVisibility(View.GONE); |
Michael Kolb | 5e8f2b9 | 2011-07-19 13:22:32 -0700 | [diff] [blame] | 220 | setSearchMode(mInVoiceMode); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 221 | mTabSwitcher.setVisibility(View.VISIBLE); |
| 222 | mTitleContainer.setBackgroundDrawable(null); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 223 | mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 224 | break; |
| 225 | case StateListener.STATE_HIGHLIGHTED: |
| 226 | mComboIcon.setVisibility(View.GONE); |
| 227 | mStopButton.setVisibility(View.VISIBLE); |
| 228 | setSearchMode(true); |
| 229 | mTabSwitcher.setVisibility(View.GONE); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 230 | mMore.setVisibility(View.GONE); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 231 | mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable); |
| 232 | break; |
| 233 | case StateListener.STATE_EDITED: |
| 234 | mComboIcon.setVisibility(View.GONE); |
| 235 | mStopButton.setVisibility(View.GONE); |
| 236 | setSearchMode(false); |
| 237 | mTabSwitcher.setVisibility(View.GONE); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 238 | mMore.setVisibility(View.GONE); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 239 | mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable); |
| 240 | break; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 243 | } |