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 | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 48 | private boolean mMenuShowing; |
| 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 | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 80 | mTextfieldBgDrawable = res.getDrawable(R.drawable.textfield_active_holo_dark); |
| 81 | setUaSwitcher(mComboIcon); |
John Reck | 67f3363 | 2011-06-17 16:23:42 -0700 | [diff] [blame] | 82 | mUrlInput.setContainer(this); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 83 | mUrlInput.setStateListener(this); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 84 | mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey(); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 88 | public void createContextMenu(ContextMenu menu) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 89 | Activity activity = mBaseUi.getActivity(); |
| 90 | MenuInflater inflater = activity.getMenuInflater(); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 91 | inflater.inflate(R.menu.title_context, menu); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 92 | activity.onCreateContextMenu(menu, this, null); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 93 | } |
| 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 Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 104 | 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 Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 111 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 112 | } |
| 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 Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 122 | } |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 125 | /** |
| 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 Reck | 67f3363 | 2011-06-17 16:23:42 -0700 | [diff] [blame] | 132 | 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 Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 143 | public void onClick(View v) { |
| 144 | if (v == mStopButton) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 145 | if (mTitleBar.isInLoad()) { |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 146 | mUiController.stopLoading(); |
| 147 | } else { |
| 148 | WebView web = mBaseUi.getWebView(); |
| 149 | if (web != null) { |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 150 | stopEditingUrl(); |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 151 | web.reload(); |
| 152 | } |
| 153 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 154 | } else if (v == mVoiceButton) { |
| 155 | mUiController.startVoiceSearch(); |
John Reck | 8ac4290 | 2011-06-29 16:14:34 -0700 | [diff] [blame] | 156 | } else if (v == mTabSwitcher) { |
| 157 | mBaseUi.onMenuKey(); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 158 | } else if (mMore == v) { |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 159 | showMenu(mMore); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 160 | } else { |
| 161 | super.onClick(v); |
| 162 | } |
| 163 | } |
| 164 | |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 165 | public boolean isMenuShowing() { |
| 166 | return mMenuShowing; |
| 167 | } |
| 168 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 169 | void showMenu(View anchor) { |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 170 | mMenuShowing = true; |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 171 | PopupMenu popup = new PopupMenu(mContext, anchor); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 172 | Menu menu = popup.getMenu(); |
| 173 | popup.getMenuInflater().inflate(R.menu.browser, menu); |
Michael Kolb | 4bf7971 | 2011-07-14 14:18:12 -0700 | [diff] [blame] | 174 | mUiController.updateMenuState(mBaseUi.getActiveTab(), menu); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 175 | popup.setOnMenuItemClickListener(this); |
| 176 | popup.setOnDismissListener(this); |
| 177 | popup.show(); |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public void onDismiss(PopupMenu menu) { |
| 182 | onMenuHidden(); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public boolean onMenuItemClick(MenuItem item) { |
| 187 | onMenuHidden(); |
| 188 | boolean res = mUiController.onOptionsItemSelected(item); |
| 189 | if (!res) { |
| 190 | return super.onMenuItemClick(item); |
| 191 | } |
| 192 | return res; |
| 193 | } |
| 194 | |
| 195 | private void onMenuHidden() { |
| 196 | mMenuShowing = false; |
| 197 | mBaseUi.showTitleBarForDuration(); |
| 198 | } |
| 199 | |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 200 | @Override |
| 201 | public void onStateChanged(int state) { |
| 202 | switch(state) { |
| 203 | case StateListener.STATE_NORMAL: |
| 204 | mComboIcon.setVisibility(View.VISIBLE); |
| 205 | mStopButton.setVisibility(View.GONE); |
| 206 | setSearchMode(false); |
| 207 | mTabSwitcher.setVisibility(View.VISIBLE); |
| 208 | mTitleContainer.setBackgroundDrawable(null); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 209 | mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 210 | break; |
| 211 | case StateListener.STATE_HIGHLIGHTED: |
| 212 | mComboIcon.setVisibility(View.GONE); |
| 213 | mStopButton.setVisibility(View.VISIBLE); |
| 214 | setSearchMode(true); |
| 215 | mTabSwitcher.setVisibility(View.GONE); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 216 | mMore.setVisibility(View.GONE); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 217 | mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable); |
| 218 | break; |
| 219 | case StateListener.STATE_EDITED: |
| 220 | mComboIcon.setVisibility(View.GONE); |
| 221 | mStopButton.setVisibility(View.GONE); |
| 222 | setSearchMode(false); |
| 223 | mTabSwitcher.setVisibility(View.GONE); |
Michael Kolb | 017ffab | 2011-07-11 15:26:47 -0700 | [diff] [blame] | 224 | mMore.setVisibility(View.GONE); |
Michael Kolb | 305b1c5 | 2011-06-21 16:16:22 -0700 | [diff] [blame] | 225 | mTitleContainer.setBackgroundDrawable(mTextfieldBgDrawable); |
| 226 | break; |
Michael Kolb | 2814a36 | 2011-05-19 15:49:41 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 229 | } |