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