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