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; |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 25 | import android.text.Editable; |
| 26 | import android.text.TextWatcher; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 27 | import android.util.AttributeSet; |
| 28 | import android.view.KeyEvent; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 29 | import android.view.View; |
| 30 | import android.view.View.OnClickListener; |
| 31 | import android.view.View.OnFocusChangeListener; |
John Reck | fa6177a | 2011-11-15 14:51:11 -0800 | [diff] [blame] | 32 | import android.webkit.WebView; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 33 | import android.widget.ImageView; |
| 34 | import android.widget.LinearLayout; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 35 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 36 | import com.android.browser.UrlInputView.UrlInputListener; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 37 | |
| 38 | import java.util.List; |
| 39 | |
John Reck | 42229bc | 2011-08-19 13:26:43 -0700 | [diff] [blame] | 40 | public class NavigationBarBase extends LinearLayout implements |
| 41 | OnClickListener, UrlInputListener, OnFocusChangeListener, |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 42 | TextWatcher { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 43 | |
| 44 | protected BaseUi mBaseUi; |
| 45 | protected TitleBar mTitleBar; |
| 46 | protected UiController mUiController; |
| 47 | protected UrlInputView mUrlInput; |
| 48 | protected boolean mInVoiceMode = false; |
| 49 | |
| 50 | private ImageView mFavicon; |
| 51 | private ImageView mLockIcon; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 52 | |
| 53 | public NavigationBarBase(Context context) { |
| 54 | super(context); |
| 55 | } |
| 56 | |
| 57 | public NavigationBarBase(Context context, AttributeSet attrs) { |
| 58 | super(context, attrs); |
| 59 | } |
| 60 | |
| 61 | public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) { |
| 62 | super(context, attrs, defStyle); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | protected void onFinishInflate() { |
| 67 | super.onFinishInflate(); |
| 68 | mLockIcon = (ImageView) findViewById(R.id.lock); |
| 69 | mFavicon = (ImageView) findViewById(R.id.favicon); |
| 70 | mUrlInput = (UrlInputView) findViewById(R.id.url); |
| 71 | mUrlInput.setUrlInputListener(this); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 72 | mUrlInput.setOnFocusChangeListener(this); |
| 73 | mUrlInput.setSelectAllOnFocus(true); |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 74 | mUrlInput.addTextChangedListener(this); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | public void setTitleBar(TitleBar titleBar) { |
| 78 | mTitleBar = titleBar; |
| 79 | mBaseUi = mTitleBar.getUi(); |
| 80 | mUiController = mTitleBar.getUiController(); |
Michael Kolb | b2e91fd | 2011-07-14 13:47:29 -0700 | [diff] [blame] | 81 | mUrlInput.setController(mUiController); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | public void setLock(Drawable d) { |
| 85 | if (mLockIcon == null) return; |
| 86 | if (d == null) { |
| 87 | mLockIcon.setVisibility(View.GONE); |
| 88 | } else { |
| 89 | mLockIcon.setImageDrawable(d); |
| 90 | mLockIcon.setVisibility(View.VISIBLE); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public void setFavicon(Bitmap icon) { |
| 95 | if (mFavicon == null) return; |
| 96 | mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon)); |
| 97 | } |
| 98 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 99 | @Override |
| 100 | public void onClick(View v) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onFocusChange(View view, boolean hasFocus) { |
| 105 | // if losing focus and not in touch mode, leave as is |
| 106 | if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) { |
| 107 | setFocusState(hasFocus); |
| 108 | } |
| 109 | if (hasFocus) { |
| 110 | mBaseUi.showTitleBar(); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 111 | if (mInVoiceMode) { |
| 112 | mUrlInput.forceFilter(); |
| 113 | } |
| 114 | } else if (!mUrlInput.needsUpdate()) { |
| 115 | mUrlInput.dismissDropDown(); |
| 116 | mUrlInput.hideIME(); |
| 117 | if (mUrlInput.getText().length() == 0) { |
| 118 | Tab currentTab = mUiController.getTabControl().getCurrentTab(); |
| 119 | if (currentTab != null) { |
John Reck | 434e9f8 | 2011-08-10 18:16:52 -0700 | [diff] [blame] | 120 | setDisplayTitle(currentTab.getUrl()); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | mBaseUi.suggestHideTitleBar(); |
| 124 | } |
| 125 | mUrlInput.clearNeedsUpdate(); |
| 126 | } |
| 127 | |
| 128 | protected void setFocusState(boolean focus) { |
| 129 | } |
| 130 | |
| 131 | protected void setSearchMode(boolean voiceSearchEnabled) {} |
| 132 | |
| 133 | public boolean isEditingUrl() { |
| 134 | return mUrlInput.hasFocus(); |
| 135 | } |
| 136 | |
| 137 | void stopEditingUrl() { |
| 138 | mUrlInput.clearFocus(); |
| 139 | } |
| 140 | |
| 141 | void setDisplayTitle(String title) { |
| 142 | if (!isEditingUrl()) { |
| 143 | mUrlInput.setText(title, false); |
| 144 | } |
| 145 | } |
| 146 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 147 | // voicesearch |
| 148 | |
| 149 | public void setInVoiceMode(boolean voicemode, List<String> voiceResults) { |
| 150 | mInVoiceMode = voicemode; |
| 151 | mUrlInput.setVoiceResults(voiceResults); |
| 152 | } |
| 153 | |
| 154 | void setIncognitoMode(boolean incognito) { |
| 155 | mUrlInput.setIncognitoMode(incognito); |
| 156 | } |
| 157 | |
| 158 | void clearCompletions() { |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 159 | mUrlInput.dismissDropDown(); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // UrlInputListener implementation |
| 163 | |
| 164 | /** |
| 165 | * callback from suggestion dropdown |
| 166 | * user selected a suggestion |
| 167 | */ |
| 168 | @Override |
| 169 | public void onAction(String text, String extra, String source) { |
John Reck | fa6177a | 2011-11-15 14:51:11 -0800 | [diff] [blame] | 170 | WebView currentTopWebView = mUiController.getCurrentTopWebView(); |
| 171 | if (currentTopWebView != null) { |
| 172 | currentTopWebView.requestFocus(); |
| 173 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 174 | if (UrlInputView.TYPED.equals(source)) { |
| 175 | String url = UrlUtils.smartUrlFilter(text, false); |
| 176 | Tab t = mBaseUi.getActiveTab(); |
| 177 | // Only shortcut javascript URIs for now, as there is special |
| 178 | // logic in UrlHandler for other schemas |
| 179 | if (url != null && t != null && url.startsWith("javascript:")) { |
| 180 | mUiController.loadUrl(t, url); |
| 181 | setDisplayTitle(text); |
| 182 | return; |
| 183 | } |
| 184 | } |
| 185 | Intent i = new Intent(); |
| 186 | String action = null; |
| 187 | if (UrlInputView.VOICE.equals(source)) { |
| 188 | action = RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS; |
| 189 | source = null; |
| 190 | } else { |
| 191 | action = Intent.ACTION_SEARCH; |
| 192 | } |
| 193 | i.setAction(action); |
| 194 | i.putExtra(SearchManager.QUERY, text); |
| 195 | if (extra != null) { |
| 196 | i.putExtra(SearchManager.EXTRA_DATA_KEY, extra); |
| 197 | } |
| 198 | if (source != null) { |
| 199 | Bundle appData = new Bundle(); |
| 200 | appData.putString(com.android.common.Search.SOURCE, source); |
| 201 | i.putExtra(SearchManager.APP_DATA, appData); |
| 202 | } |
| 203 | mUiController.handleNewIntent(i); |
| 204 | setDisplayTitle(text); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void onDismiss() { |
| 209 | final Tab currentTab = mBaseUi.getActiveTab(); |
| 210 | mBaseUi.hideTitleBar(); |
| 211 | post(new Runnable() { |
| 212 | public void run() { |
| 213 | clearFocus(); |
| 214 | if ((currentTab != null) && !mInVoiceMode) { |
| 215 | setDisplayTitle(currentTab.getUrl()); |
| 216 | } |
| 217 | } |
| 218 | }); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * callback from the suggestion dropdown |
| 223 | * copy text to input field and stay in edit mode |
| 224 | */ |
| 225 | @Override |
| 226 | public void onCopySuggestion(String text) { |
| 227 | mUrlInput.setText(text, true); |
| 228 | if (text != null) { |
| 229 | mUrlInput.setSelection(text.length()); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | public void setCurrentUrlIsBookmark(boolean isBookmark) { |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public boolean dispatchKeyEventPreIme(KeyEvent evt) { |
| 238 | if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) { |
| 239 | // catch back key in order to do slightly more cleanup than usual |
| 240 | mUrlInput.clearFocus(); |
| 241 | return true; |
| 242 | } |
| 243 | return super.dispatchKeyEventPreIme(evt); |
| 244 | } |
| 245 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 246 | /** |
| 247 | * called from the Ui when the user wants to edit |
| 248 | * @param clearInput clear the input field |
| 249 | */ |
Michael Kolb | 1f9b356 | 2012-04-24 14:38:34 -0700 | [diff] [blame] | 250 | void startEditingUrl(boolean clearInput, boolean forceIME) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 251 | // editing takes preference of progress |
| 252 | setVisibility(View.VISIBLE); |
| 253 | if (mTitleBar.useQuickControls()) { |
| 254 | mTitleBar.getProgressView().setVisibility(View.GONE); |
| 255 | } |
| 256 | if (!mUrlInput.hasFocus()) { |
| 257 | mUrlInput.requestFocus(); |
| 258 | } |
| 259 | if (clearInput) { |
| 260 | mUrlInput.setText(""); |
| 261 | } else if (mInVoiceMode) { |
| 262 | mUrlInput.showDropDown(); |
| 263 | } |
Michael Kolb | 1f9b356 | 2012-04-24 14:38:34 -0700 | [diff] [blame] | 264 | if (forceIME) { |
Michael Kolb | 4bb6fcb | 2012-04-13 14:25:27 -0700 | [diff] [blame] | 265 | mUrlInput.showIME(); |
| 266 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | public void onProgressStarted() { |
| 270 | } |
| 271 | |
| 272 | public void onProgressStopped() { |
| 273 | } |
| 274 | |
John Reck | 5889190 | 2011-08-11 17:48:53 -0700 | [diff] [blame] | 275 | public boolean isMenuShowing() { |
John Reck | 42229bc | 2011-08-19 13:26:43 -0700 | [diff] [blame] | 276 | return false; |
John Reck | 5889190 | 2011-08-11 17:48:53 -0700 | [diff] [blame] | 277 | } |
| 278 | |
John Reck | 419f6b4 | 2011-08-16 16:10:51 -0700 | [diff] [blame] | 279 | public void onTabDataChanged(Tab tab) { |
| 280 | } |
| 281 | |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 282 | @Override |
| 283 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { } |
| 284 | |
| 285 | @Override |
| 286 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 287 | if (mUrlInput.hasFocus()) { |
| 288 | // clear voice mode when user types |
| 289 | setInVoiceMode(false, null); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public void afterTextChanged(Editable s) { } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 295 | } |