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