John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | package com.android.browser; |
| 17 | |
| 18 | import android.app.SearchManager; |
| 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
| 21 | import android.graphics.Bitmap; |
| 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.os.Bundle; |
| 24 | import android.speech.RecognizerResultsIntent; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.view.KeyEvent; |
| 27 | import android.view.Menu; |
| 28 | import android.view.MenuItem; |
| 29 | import android.view.View; |
| 30 | import android.view.View.OnClickListener; |
| 31 | import android.view.View.OnFocusChangeListener; |
| 32 | import android.webkit.WebView; |
| 33 | import android.widget.ImageView; |
| 34 | import android.widget.LinearLayout; |
| 35 | import android.widget.PopupMenu; |
| 36 | import android.widget.PopupMenu.OnMenuItemClickListener; |
| 37 | |
| 38 | import com.android.browser.UI.DropdownChangeListener; |
| 39 | import com.android.browser.UrlInputView.UrlInputListener; |
| 40 | import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher; |
| 41 | |
| 42 | import java.util.List; |
| 43 | |
| 44 | public class NavigationBarBase extends LinearLayout implements OnClickListener, |
| 45 | OnMenuItemClickListener, UrlInputListener, OnFocusChangeListener, |
| 46 | TextChangeWatcher { |
| 47 | |
| 48 | protected BaseUi mBaseUi; |
| 49 | protected TitleBar mTitleBar; |
| 50 | protected UiController mUiController; |
| 51 | protected UrlInputView mUrlInput; |
| 52 | protected boolean mInVoiceMode = false; |
| 53 | |
| 54 | private ImageView mFavicon; |
| 55 | private ImageView mLockIcon; |
| 56 | private View mUaSwitcher; |
| 57 | |
| 58 | public NavigationBarBase(Context context) { |
| 59 | super(context); |
| 60 | } |
| 61 | |
| 62 | public NavigationBarBase(Context context, AttributeSet attrs) { |
| 63 | super(context, attrs); |
| 64 | } |
| 65 | |
| 66 | public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) { |
| 67 | super(context, attrs, defStyle); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | protected void onFinishInflate() { |
| 72 | super.onFinishInflate(); |
| 73 | mLockIcon = (ImageView) findViewById(R.id.lock); |
| 74 | mFavicon = (ImageView) findViewById(R.id.favicon); |
| 75 | mUrlInput = (UrlInputView) findViewById(R.id.url); |
| 76 | mUrlInput.setUrlInputListener(this); |
| 77 | mUrlInput.setController(mUiController); |
| 78 | mUrlInput.setOnFocusChangeListener(this); |
| 79 | mUrlInput.setSelectAllOnFocus(true); |
| 80 | mUrlInput.addQueryTextWatcher(this); |
| 81 | } |
| 82 | |
| 83 | public void setTitleBar(TitleBar titleBar) { |
| 84 | mTitleBar = titleBar; |
| 85 | mBaseUi = mTitleBar.getUi(); |
| 86 | mUiController = mTitleBar.getUiController(); |
| 87 | } |
| 88 | |
| 89 | public void setLock(Drawable d) { |
| 90 | if (mLockIcon == null) return; |
| 91 | if (d == null) { |
| 92 | mLockIcon.setVisibility(View.GONE); |
| 93 | } else { |
| 94 | mLockIcon.setImageDrawable(d); |
| 95 | mLockIcon.setVisibility(View.VISIBLE); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public void setFavicon(Bitmap icon) { |
| 100 | if (mFavicon == null) return; |
| 101 | mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon)); |
| 102 | } |
| 103 | |
| 104 | public void setUaSwitcher(View v) { |
| 105 | if (mUaSwitcher != null) { |
| 106 | mUaSwitcher.setOnClickListener(null); |
| 107 | } |
| 108 | mUaSwitcher = v; |
| 109 | mUaSwitcher.setOnClickListener(this); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void onClick(View v) { |
| 114 | if (mUaSwitcher == v) { |
| 115 | BrowserSettings settings = BrowserSettings.getInstance(); |
| 116 | WebView web = mTitleBar.getCurrentWebView(); |
| 117 | if (web == null) return; |
| 118 | boolean desktop = settings.hasDesktopUseragent(web); |
| 119 | PopupMenu popup = new PopupMenu(mContext, mUaSwitcher); |
| 120 | Menu menu = popup.getMenu(); |
| 121 | popup.getMenuInflater().inflate(R.menu.ua_switcher, menu); |
| 122 | menu.findItem(R.id.ua_mobile_menu_id).setChecked(!desktop); |
| 123 | menu.findItem(R.id.ua_desktop_menu_id).setChecked(desktop); |
| 124 | popup.setOnMenuItemClickListener(this); |
| 125 | popup.show(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public boolean onMenuItemClick(MenuItem item) { |
| 131 | BrowserSettings settings = BrowserSettings.getInstance(); |
| 132 | WebView web = mTitleBar.getCurrentWebView(); |
| 133 | if (web == null) return false; |
| 134 | boolean desktop = settings.hasDesktopUseragent(web); |
| 135 | switch (item.getItemId()) { |
| 136 | case R.id.ua_mobile_menu_id: |
| 137 | if (desktop) { |
| 138 | settings.toggleDesktopUseragent(web); |
| 139 | web.loadUrl(web.getOriginalUrl()); |
| 140 | } |
| 141 | return true; |
| 142 | case R.id.ua_desktop_menu_id: |
| 143 | if (!desktop) { |
| 144 | settings.toggleDesktopUseragent(web); |
| 145 | web.loadUrl(web.getOriginalUrl()); |
| 146 | } |
| 147 | return true; |
| 148 | } |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void onFocusChange(View view, boolean hasFocus) { |
| 154 | // if losing focus and not in touch mode, leave as is |
| 155 | if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) { |
| 156 | setFocusState(hasFocus); |
| 157 | } |
| 158 | if (hasFocus) { |
| 159 | mBaseUi.showTitleBar(); |
| 160 | mUrlInput.forceIme(); |
| 161 | if (mInVoiceMode) { |
| 162 | mUrlInput.forceFilter(); |
| 163 | } |
| 164 | } else if (!mUrlInput.needsUpdate()) { |
| 165 | mUrlInput.dismissDropDown(); |
| 166 | mUrlInput.hideIME(); |
| 167 | if (mUrlInput.getText().length() == 0) { |
| 168 | Tab currentTab = mUiController.getTabControl().getCurrentTab(); |
| 169 | if (currentTab != null) { |
| 170 | mUrlInput.setText(currentTab.getUrl(), false); |
| 171 | } |
| 172 | } |
| 173 | mBaseUi.suggestHideTitleBar(); |
| 174 | } |
| 175 | mUrlInput.clearNeedsUpdate(); |
| 176 | } |
| 177 | |
| 178 | protected void setFocusState(boolean focus) { |
| 179 | } |
| 180 | |
| 181 | protected void setSearchMode(boolean voiceSearchEnabled) {} |
| 182 | |
| 183 | public boolean isEditingUrl() { |
| 184 | return mUrlInput.hasFocus(); |
| 185 | } |
| 186 | |
| 187 | void stopEditingUrl() { |
| 188 | mUrlInput.clearFocus(); |
| 189 | } |
| 190 | |
| 191 | void setDisplayTitle(String title) { |
| 192 | if (!isEditingUrl()) { |
| 193 | mUrlInput.setText(title, false); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // UrlInput text watcher |
| 198 | |
| 199 | @Override |
| 200 | public void onTextChanged(String newText) { |
| 201 | if (mUrlInput.hasFocus()) { |
| 202 | // clear voice mode when user types |
| 203 | setInVoiceMode(false, null); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // voicesearch |
| 208 | |
| 209 | public void setInVoiceMode(boolean voicemode, List<String> voiceResults) { |
| 210 | mInVoiceMode = voicemode; |
| 211 | mUrlInput.setVoiceResults(voiceResults); |
| 212 | } |
| 213 | |
| 214 | void setIncognitoMode(boolean incognito) { |
| 215 | mUrlInput.setIncognitoMode(incognito); |
| 216 | } |
| 217 | |
| 218 | void clearCompletions() { |
| 219 | mUrlInput.setSuggestedText(null); |
| 220 | } |
| 221 | |
| 222 | // UrlInputListener implementation |
| 223 | |
| 224 | /** |
| 225 | * callback from suggestion dropdown |
| 226 | * user selected a suggestion |
| 227 | */ |
| 228 | @Override |
| 229 | public void onAction(String text, String extra, String source) { |
| 230 | mUiController.getCurrentTopWebView().requestFocus(); |
| 231 | if (UrlInputView.TYPED.equals(source)) { |
| 232 | String url = UrlUtils.smartUrlFilter(text, false); |
| 233 | Tab t = mBaseUi.getActiveTab(); |
| 234 | // Only shortcut javascript URIs for now, as there is special |
| 235 | // logic in UrlHandler for other schemas |
| 236 | if (url != null && t != null && url.startsWith("javascript:")) { |
| 237 | mUiController.loadUrl(t, url); |
| 238 | setDisplayTitle(text); |
| 239 | return; |
| 240 | } |
| 241 | } |
| 242 | Intent i = new Intent(); |
| 243 | String action = null; |
| 244 | if (UrlInputView.VOICE.equals(source)) { |
| 245 | action = RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS; |
| 246 | source = null; |
| 247 | } else { |
| 248 | action = Intent.ACTION_SEARCH; |
| 249 | } |
| 250 | i.setAction(action); |
| 251 | i.putExtra(SearchManager.QUERY, text); |
| 252 | if (extra != null) { |
| 253 | i.putExtra(SearchManager.EXTRA_DATA_KEY, extra); |
| 254 | } |
| 255 | if (source != null) { |
| 256 | Bundle appData = new Bundle(); |
| 257 | appData.putString(com.android.common.Search.SOURCE, source); |
| 258 | i.putExtra(SearchManager.APP_DATA, appData); |
| 259 | } |
| 260 | mUiController.handleNewIntent(i); |
| 261 | setDisplayTitle(text); |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void onDismiss() { |
| 266 | final Tab currentTab = mBaseUi.getActiveTab(); |
| 267 | mBaseUi.hideTitleBar(); |
| 268 | post(new Runnable() { |
| 269 | public void run() { |
| 270 | clearFocus(); |
| 271 | if ((currentTab != null) && !mInVoiceMode) { |
| 272 | setDisplayTitle(currentTab.getUrl()); |
| 273 | } |
| 274 | } |
| 275 | }); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * callback from the suggestion dropdown |
| 280 | * copy text to input field and stay in edit mode |
| 281 | */ |
| 282 | @Override |
| 283 | public void onCopySuggestion(String text) { |
| 284 | mUrlInput.setText(text, true); |
| 285 | if (text != null) { |
| 286 | mUrlInput.setSelection(text.length()); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | public void setCurrentUrlIsBookmark(boolean isBookmark) { |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public boolean dispatchKeyEventPreIme(KeyEvent evt) { |
| 295 | if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) { |
| 296 | // catch back key in order to do slightly more cleanup than usual |
| 297 | mUrlInput.clearFocus(); |
| 298 | return true; |
| 299 | } |
| 300 | return super.dispatchKeyEventPreIme(evt); |
| 301 | } |
| 302 | |
| 303 | void registerDropdownChangeListener(DropdownChangeListener d) { |
| 304 | mUrlInput.registerDropdownChangeListener(d); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * called from the Ui when the user wants to edit |
| 309 | * @param clearInput clear the input field |
| 310 | */ |
| 311 | void startEditingUrl(boolean clearInput) { |
| 312 | // editing takes preference of progress |
| 313 | setVisibility(View.VISIBLE); |
| 314 | if (mTitleBar.useQuickControls()) { |
| 315 | mTitleBar.getProgressView().setVisibility(View.GONE); |
| 316 | } |
| 317 | if (!mUrlInput.hasFocus()) { |
| 318 | mUrlInput.requestFocus(); |
| 319 | } |
| 320 | if (clearInput) { |
| 321 | mUrlInput.setText(""); |
| 322 | } else if (mInVoiceMode) { |
| 323 | mUrlInput.showDropDown(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | public void onProgressStarted() { |
| 328 | } |
| 329 | |
| 330 | public void onProgressStopped() { |
| 331 | } |
| 332 | |
| 333 | } |