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 | */ |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 16 | package com.android.browser; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 17 | |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 18 | import android.animation.ArgbEvaluator; |
| 19 | import android.animation.ValueAnimator; |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 20 | import android.app.Activity; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 21 | import android.app.SearchManager; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 22 | import android.content.ActivityNotFoundException; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 25 | import android.content.SharedPreferences; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 26 | import android.graphics.Bitmap; |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 27 | import android.graphics.Color; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 29 | import android.net.Uri; |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 30 | import android.net.http.SslCertificate; |
| 31 | import android.net.http.SslError; |
| 32 | import android.os.Build; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 33 | import android.os.Bundle; |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 34 | import android.text.Editable; |
| 35 | import android.text.TextWatcher; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 36 | import android.util.AttributeSet; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 37 | import android.util.Log; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 38 | import android.view.KeyEvent; |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 39 | import android.view.Menu; |
| 40 | import android.view.MenuItem; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 41 | import android.view.View; |
| 42 | import android.view.View.OnClickListener; |
| 43 | import android.view.View.OnFocusChangeListener; |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 44 | import android.view.ViewConfiguration; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 45 | import android.widget.ImageView; |
| 46 | import android.widget.LinearLayout; |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 47 | import android.widget.PopupMenu; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 48 | import android.widget.Toast; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 49 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 50 | import com.android.browser.UrlInputView.UrlInputListener; |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 51 | import com.android.browser.preferences.SiteSpecificPreferencesFragment; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 52 | |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 53 | import java.io.ByteArrayOutputStream; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 54 | import java.io.UnsupportedEncodingException; |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 55 | import java.net.MalformedURLException; |
kaiyiz | 950eca2 | 2013-08-08 11:01:28 +0800 | [diff] [blame] | 56 | import java.net.URISyntaxException; |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 57 | import java.net.URL; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 58 | |
Kevin Hart | 055e6d8 | 2014-12-19 15:53:19 -0800 | [diff] [blame] | 59 | import org.codeaurora.swe.Engine; |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 60 | import org.codeaurora.swe.WebRefiner; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 61 | import org.codeaurora.swe.WebView; |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 62 | import org.codeaurora.swe.util.ColorUtils; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 63 | |
John Reck | 42229bc | 2011-08-19 13:26:43 -0700 | [diff] [blame] | 64 | public class NavigationBarBase extends LinearLayout implements |
| 65 | OnClickListener, UrlInputListener, OnFocusChangeListener, |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 66 | TextWatcher, UrlInputView.StateListener, |
| 67 | PopupMenu.OnMenuItemClickListener, PopupMenu.OnDismissListener { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 68 | |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 69 | private final static String TAG = "NavigationBarBase"; |
| 70 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 71 | protected BaseUi mBaseUi; |
| 72 | protected TitleBar mTitleBar; |
| 73 | protected UiController mUiController; |
| 74 | protected UrlInputView mUrlInput; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 75 | |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 76 | protected ImageView mFaviconBadge; |
| 77 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 78 | private ImageView mFavicon; |
| 79 | private ImageView mLockIcon; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 80 | |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 81 | private View mMore; |
| 82 | private PopupMenu mPopupMenu; |
| 83 | private boolean mOverflowMenuShowing; |
| 84 | private boolean mNeedsMenu; |
Axesh R. Ajmera | 2e24124 | 2014-05-19 15:53:38 -0700 | [diff] [blame] | 85 | |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 86 | private int mStatusBarColor; |
| 87 | private static int mDefaultStatusBarColor = -1; |
| 88 | |
| 89 | private Tab.SecurityState mSecurityState = Tab.SecurityState.SECURITY_STATE_NOT_SECURE; |
| 90 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 91 | public NavigationBarBase(Context context) { |
| 92 | super(context); |
| 93 | } |
| 94 | |
| 95 | public NavigationBarBase(Context context, AttributeSet attrs) { |
| 96 | super(context, attrs); |
| 97 | } |
| 98 | |
| 99 | public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) { |
| 100 | super(context, attrs, defStyle); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | protected void onFinishInflate() { |
| 105 | super.onFinishInflate(); |
| 106 | mLockIcon = (ImageView) findViewById(R.id.lock); |
| 107 | mFavicon = (ImageView) findViewById(R.id.favicon); |
| 108 | mUrlInput = (UrlInputView) findViewById(R.id.url); |
| 109 | mUrlInput.setUrlInputListener(this); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 110 | mUrlInput.setOnFocusChangeListener(this); |
| 111 | mUrlInput.setSelectAllOnFocus(true); |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 112 | mUrlInput.addTextChangedListener(this); |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 113 | mMore = findViewById(R.id.more_browser_settings); |
| 114 | mMore.setOnClickListener(this); |
| 115 | mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey(); |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 116 | mFaviconBadge = (ImageView) findViewById(R.id.favicon_badge); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | public void setTitleBar(TitleBar titleBar) { |
| 120 | mTitleBar = titleBar; |
| 121 | mBaseUi = mTitleBar.getUi(); |
| 122 | mUiController = mTitleBar.getUiController(); |
Michael Kolb | b2e91fd | 2011-07-14 13:47:29 -0700 | [diff] [blame] | 123 | mUrlInput.setController(mUiController); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 126 | public void setLock(Drawable d, Tab.SecurityState securityState) { |
| 127 | mSecurityState = securityState; |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame] | 128 | if (mFaviconBadge != null) { |
| 129 | switch (mSecurityState) { |
| 130 | case SECURITY_STATE_SECURE: |
| 131 | mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_good); |
| 132 | break; |
| 133 | case SECURITY_STATE_MIXED: |
| 134 | mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_warning); |
| 135 | break; |
| 136 | case SECURITY_STATE_BAD_CERTIFICATE: |
| 137 | mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_severe); |
| 138 | break; |
| 139 | case SECURITY_STATE_NOT_SECURE: |
| 140 | default: |
| 141 | mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_normal); |
| 142 | } |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 143 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 144 | if (mLockIcon == null) return; |
| 145 | if (d == null) { |
| 146 | mLockIcon.setVisibility(View.GONE); |
| 147 | } else { |
| 148 | mLockIcon.setImageDrawable(d); |
| 149 | mLockIcon.setVisibility(View.VISIBLE); |
| 150 | } |
| 151 | } |
| 152 | |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 153 | public static int adjustColor(int color, float hueMultiplier, |
| 154 | float saturationMultiplier, float valueMultiplier) { |
| 155 | float[] hsv = new float[3]; |
| 156 | Color.colorToHSV(color, hsv); |
| 157 | hsv[0] *= hueMultiplier; |
| 158 | hsv[1] *= saturationMultiplier; |
| 159 | hsv[2] *= valueMultiplier; |
| 160 | return Color.HSVToColor(Color.alpha(color), hsv); |
| 161 | } |
| 162 | |
| 163 | public static void setStatusAndNavigationBarColor(final Activity activity, int color) { |
| 164 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 165 | int currentColor = activity.getWindow().getStatusBarColor(); |
| 166 | Integer from = currentColor; |
| 167 | Integer to = color; |
| 168 | ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to); |
| 169 | |
| 170 | if (mDefaultStatusBarColor == -1) { |
| 171 | mDefaultStatusBarColor = activity.getWindow().getStatusBarColor(); |
| 172 | } |
| 173 | |
| 174 | animator.addUpdateListener( |
| 175 | new ValueAnimator.AnimatorUpdateListener() { |
| 176 | @Override |
| 177 | public void onAnimationUpdate(ValueAnimator animation) { |
| 178 | Integer value = (Integer) animation.getAnimatedValue(); |
| 179 | activity.getWindow().setStatusBarColor(value.intValue()); |
| 180 | //activity.getWindow().setNavigationBarColor(value.intValue()); |
| 181 | } |
| 182 | } |
| 183 | ); |
| 184 | animator.start(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | private void updateSiteIconColor(String urlString, int color) { |
| 189 | try { |
| 190 | URL url = new URL(urlString); |
| 191 | String host = url.getHost(); |
| 192 | SharedPreferences prefs = BrowserSettings.getInstance().getPreferences(); |
| 193 | int currentColor = prefs.getInt(host + ":color", 0); |
| 194 | if (currentColor != color) { |
| 195 | SharedPreferences.Editor editor = prefs.edit(); |
| 196 | editor.remove(host + ":color"); |
| 197 | editor.putInt(host + ":color", color); |
| 198 | editor.commit(); |
| 199 | } |
| 200 | } catch (MalformedURLException e) { |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | public static int getSiteIconColor(String urlString) { |
| 205 | try { |
| 206 | URL url = new URL(urlString); |
| 207 | String host = url.getHost(); |
| 208 | SharedPreferences prefs = BrowserSettings.getInstance().getPreferences(); |
| 209 | return prefs.getInt(host + ":color", 0); |
| 210 | } catch (MalformedURLException e) { |
| 211 | return 0; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | public static int getDefaultStatusBarColor() { |
| 216 | return mDefaultStatusBarColor; |
| 217 | } |
| 218 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 219 | public void setFavicon(Bitmap icon) { |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 220 | int color = ColorUtils.getDominantColorForBitmap(icon); |
| 221 | Tab tab = mUiController.getCurrentTab(); |
| 222 | |
| 223 | if (tab != null) { |
| 224 | if (tab.hasFavicon()) { |
| 225 | updateSiteIconColor(tab.getUrl(), color); |
| 226 | setStatusAndNavigationBarColor(mUiController.getActivity(), |
| 227 | adjustColor(color, 1, 1, 0.7f)); |
| 228 | } else { |
| 229 | color = getSiteIconColor(tab.getUrl()); |
| 230 | if (color != 0) { |
| 231 | setStatusAndNavigationBarColor(mUiController.getActivity(), |
| 232 | adjustColor(color, 1, 1, 0.7f)); |
| 233 | } else { |
| 234 | setStatusAndNavigationBarColor(mUiController.getActivity(), |
| 235 | mDefaultStatusBarColor); |
| 236 | } |
| 237 | } |
| 238 | } else { |
| 239 | setStatusAndNavigationBarColor(mUiController.getActivity(), mDefaultStatusBarColor); |
| 240 | } |
| 241 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 242 | if (mFavicon == null) return; |
| 243 | mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon)); |
| 244 | } |
| 245 | |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 246 | protected void showSiteSpecificSettings() { |
| 247 | WebView wv = mUiController.getCurrentTopWebView(); |
| 248 | int count = 0; |
| 249 | |
| 250 | if (wv != null && WebRefiner.getInstance() != null) { |
| 251 | count = WebRefiner.getInstance().getBlockedURLCount(wv); |
| 252 | } |
| 253 | |
| 254 | Bundle bundle = new Bundle(); |
| 255 | bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE, |
| 256 | mUiController.getCurrentTab().getUrl()); |
| 257 | bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_INFO, count); |
| 258 | |
| 259 | bundle.putParcelable(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT, |
| 260 | SslCertificate.saveState(wv.getCertificate())); |
| 261 | |
| 262 | SslError error = mUiController.getCurrentTab().getSslCertificateError(); |
| 263 | if (error != null) { |
| 264 | int certError = 0; |
| 265 | if (error.hasError(SslError.SSL_DATE_INVALID)) { |
| 266 | certError |= (1 << SslError.SSL_DATE_INVALID); |
| 267 | } |
| 268 | |
| 269 | if (error.hasError(SslError.SSL_EXPIRED)) { |
| 270 | certError |= (1 << SslError.SSL_EXPIRED); |
| 271 | } |
| 272 | |
| 273 | if (error.hasError(SslError.SSL_IDMISMATCH)) { |
| 274 | certError |= (1 << SslError.SSL_IDMISMATCH); |
| 275 | } |
| 276 | |
| 277 | if (error.hasError(SslError.SSL_INVALID)) { |
| 278 | certError |= (1 << SslError.SSL_INVALID); |
| 279 | } |
| 280 | |
| 281 | if (error.hasError(SslError.SSL_NOTYETVALID)) { |
| 282 | certError |= (1 << SslError.SSL_NOTYETVALID); |
| 283 | } |
| 284 | |
| 285 | if (error.hasError(SslError.SSL_UNTRUSTED)) { |
| 286 | certError |= (1 << SslError.SSL_UNTRUSTED); |
| 287 | } |
| 288 | |
| 289 | bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_ERR, certError); |
| 290 | } |
| 291 | |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame] | 292 | Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon(); |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 293 | if (favicon != null) { |
| 294 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 295 | favicon.compress(Bitmap.CompressFormat.PNG, 50, baos); |
| 296 | bundle.putByteArray(SiteSpecificPreferencesFragment.EXTRA_FAVICON, |
| 297 | baos.toByteArray()); |
| 298 | } |
| 299 | BrowserPreferencesPage.startPreferenceFragmentExtraForResult( |
| 300 | mUiController.getActivity(), |
| 301 | SiteSpecificPreferencesFragment.class.getName(), bundle, 0); |
| 302 | } |
| 303 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 304 | @Override |
| 305 | public void onClick(View v) { |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 306 | if (mMore == v) { |
| 307 | showMenu(mMore); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | void showMenu(View anchor) { |
| 312 | Activity activity = mUiController.getActivity(); |
| 313 | if (mPopupMenu == null) { |
| 314 | mPopupMenu = new PopupMenu(getContext(), anchor); |
| 315 | mPopupMenu.setOnMenuItemClickListener(this); |
| 316 | mPopupMenu.setOnDismissListener(this); |
| 317 | if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) { |
| 318 | mPopupMenu = null; |
| 319 | return; |
| 320 | } |
| 321 | } |
| 322 | Menu menu = mPopupMenu.getMenu(); |
| 323 | |
| 324 | if (mUiController instanceof Controller) { |
| 325 | Controller controller = (Controller) mUiController; |
| 326 | if (controller.onPrepareOptionsMenu(menu)) { |
| 327 | mOverflowMenuShowing = true; |
| 328 | } |
| 329 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public void onFocusChange(View view, boolean hasFocus) { |
| 334 | // if losing focus and not in touch mode, leave as is |
| 335 | if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) { |
| 336 | setFocusState(hasFocus); |
| 337 | } |
| 338 | if (hasFocus) { |
Kevin Hart | 055e6d8 | 2014-12-19 15:53:19 -0800 | [diff] [blame] | 339 | Engine.warmUpChildProcessAsync(mUiController.getActivity().getApplicationContext()); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 340 | mBaseUi.showTitleBar(); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 341 | } else if (!mUrlInput.needsUpdate()) { |
| 342 | mUrlInput.dismissDropDown(); |
| 343 | mUrlInput.hideIME(); |
| 344 | if (mUrlInput.getText().length() == 0) { |
| 345 | Tab currentTab = mUiController.getTabControl().getCurrentTab(); |
| 346 | if (currentTab != null) { |
John Reck | 434e9f8 | 2011-08-10 18:16:52 -0700 | [diff] [blame] | 347 | setDisplayTitle(currentTab.getUrl()); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | mBaseUi.suggestHideTitleBar(); |
| 351 | } |
| 352 | mUrlInput.clearNeedsUpdate(); |
| 353 | } |
| 354 | |
| 355 | protected void setFocusState(boolean focus) { |
| 356 | } |
| 357 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 358 | public boolean isEditingUrl() { |
| 359 | return mUrlInput.hasFocus(); |
| 360 | } |
| 361 | |
| 362 | void stopEditingUrl() { |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 363 | WebView currentTopWebView = mUiController.getCurrentTopWebView(); |
| 364 | if (currentTopWebView != null) { |
| 365 | currentTopWebView.requestFocus(); |
| 366 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void setDisplayTitle(String title) { |
| 370 | if (!isEditingUrl()) { |
Michael Kolb | 29aab40 | 2012-05-29 17:22:35 -0700 | [diff] [blame] | 371 | if (!title.equals(mUrlInput.getText().toString())) { |
| 372 | mUrlInput.setText(title, false); |
| 373 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 377 | void setIncognitoMode(boolean incognito) { |
| 378 | mUrlInput.setIncognitoMode(incognito); |
| 379 | } |
| 380 | |
| 381 | void clearCompletions() { |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 382 | mUrlInput.dismissDropDown(); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | // UrlInputListener implementation |
| 386 | |
| 387 | /** |
| 388 | * callback from suggestion dropdown |
| 389 | * user selected a suggestion |
| 390 | */ |
| 391 | @Override |
| 392 | public void onAction(String text, String extra, String source) { |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 393 | stopEditingUrl(); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 394 | if (UrlInputView.TYPED.equals(source)) { |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 395 | String url = null; |
Panos Thomas | 4bdb525 | 2014-11-13 16:20:11 -0800 | [diff] [blame] | 396 | boolean wap2estore = BrowserConfig.getInstance(getContext()) |
| 397 | .hasFeature(BrowserConfig.Feature.WAP2ESTORE); |
Vivek Sekhar | 5e63147 | 2014-03-31 23:59:10 -0700 | [diff] [blame] | 398 | if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text) |
| 399 | || isMakeCallTypeUrl(text)) { |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 400 | url = text; |
| 401 | } else { |
| 402 | url = UrlUtils.smartUrlFilter(text, false); |
| 403 | } |
| 404 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 405 | Tab t = mBaseUi.getActiveTab(); |
| 406 | // Only shortcut javascript URIs for now, as there is special |
| 407 | // logic in UrlHandler for other schemas |
| 408 | if (url != null && t != null && url.startsWith("javascript:")) { |
| 409 | mUiController.loadUrl(t, url); |
| 410 | setDisplayTitle(text); |
| 411 | return; |
| 412 | } |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 413 | |
| 414 | // add for carrier wap2estore feature |
| 415 | if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) { |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 416 | if (handleEstoreTypeUrl(url)) { |
| 417 | setDisplayTitle(text); |
| 418 | return; |
| 419 | } |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 420 | } |
kaiyiz | 950eca2 | 2013-08-08 11:01:28 +0800 | [diff] [blame] | 421 | // add for rtsp scheme feature |
| 422 | if (url != null && t != null && isRtspTypeUrl(url)) { |
| 423 | if (handleRtspTypeUrl(url)) { |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 424 | setDisplayTitle(text); |
kaiyiz | 950eca2 | 2013-08-08 11:01:28 +0800 | [diff] [blame] | 425 | return; |
| 426 | } |
| 427 | } |
Vivek Sekhar | 5e63147 | 2014-03-31 23:59:10 -0700 | [diff] [blame] | 428 | // add for "wtai://wp/mc;" scheme feature |
| 429 | if (url != null && t != null && isMakeCallTypeUrl(url)) { |
| 430 | if (handleMakeCallTypeUrl(url)) { |
| 431 | return; |
| 432 | } |
| 433 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 434 | } |
| 435 | Intent i = new Intent(); |
Michael Kolb | 5ff5c8b | 2012-05-03 11:37:58 -0700 | [diff] [blame] | 436 | String action = Intent.ACTION_SEARCH; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 437 | i.setAction(action); |
| 438 | i.putExtra(SearchManager.QUERY, text); |
| 439 | if (extra != null) { |
| 440 | i.putExtra(SearchManager.EXTRA_DATA_KEY, extra); |
| 441 | } |
| 442 | if (source != null) { |
| 443 | Bundle appData = new Bundle(); |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 444 | appData.putString("source", source); |
| 445 | i.putExtra("source", appData); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 446 | } |
| 447 | mUiController.handleNewIntent(i); |
| 448 | setDisplayTitle(text); |
| 449 | } |
| 450 | |
Vivek Sekhar | 5e63147 | 2014-03-31 23:59:10 -0700 | [diff] [blame] | 451 | private boolean isMakeCallTypeUrl(String url) { |
| 452 | String utf8Url = null; |
| 453 | try { |
| 454 | utf8Url = new String(url.getBytes("UTF-8"), "UTF-8"); |
| 455 | } catch (UnsupportedEncodingException e) { |
| 456 | Log.e(TAG, "err " + e); |
| 457 | } |
| 458 | if (utf8Url != null && utf8Url.startsWith(UrlHandler.SCHEME_WTAI_MC)) { |
| 459 | return true; |
| 460 | } |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | private boolean handleMakeCallTypeUrl(String url) { |
| 465 | // wtai://wp/mc;number |
| 466 | // number=string(phone-number) |
| 467 | if (url.startsWith(UrlHandler.SCHEME_WTAI_MC)) { |
| 468 | Intent intent = new Intent(Intent.ACTION_VIEW, |
| 469 | Uri.parse(WebView.SCHEME_TEL + |
| 470 | url.substring(UrlHandler.SCHEME_WTAI_MC.length()))); |
| 471 | getContext().startActivity(intent); |
| 472 | // before leaving BrowserActivity, close the empty child tab. |
| 473 | // If a new tab is created through JavaScript open to load this |
| 474 | // url, we would like to close it as we will load this url in a |
| 475 | // different Activity. |
| 476 | Tab current = mUiController.getCurrentTab(); |
| 477 | if (current != null |
| 478 | && current.getWebView().copyBackForwardList().getSize() == 0) { |
| 479 | mUiController.closeCurrentTab(); |
| 480 | } |
| 481 | return true; |
| 482 | } |
| 483 | return false; |
| 484 | } |
| 485 | |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 486 | private boolean isEstoreTypeUrl(String url) { |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 487 | if (url != null && url.startsWith("estore:")) { |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 488 | return true; |
| 489 | } |
| 490 | return false; |
| 491 | } |
| 492 | |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 493 | private boolean handleEstoreTypeUrl(String url) { |
| 494 | if (url.getBytes().length > 256) { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 495 | Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show(); |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 496 | return false; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 497 | } |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 498 | |
| 499 | Intent intent; |
| 500 | // perform generic parsing of the URI to turn it into an Intent. |
| 501 | try { |
| 502 | intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
| 503 | } catch (URISyntaxException ex) { |
| 504 | Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage()); |
| 505 | return false; |
| 506 | } |
| 507 | |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 508 | try { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 509 | getContext().startActivity(intent); |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 510 | } catch (ActivityNotFoundException ex) { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 511 | String downloadUrl = getContext().getResources().getString(R.string.estore_homepage); |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 512 | mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl); |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 513 | Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show(); |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 514 | } |
Vivek Sekhar | b54614f | 2014-05-01 19:03:37 -0700 | [diff] [blame] | 515 | |
| 516 | return true; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 517 | } |
| 518 | |
kaiyiz | 950eca2 | 2013-08-08 11:01:28 +0800 | [diff] [blame] | 519 | private boolean isRtspTypeUrl(String url) { |
| 520 | String utf8Url = null; |
| 521 | try { |
| 522 | utf8Url = new String(url.getBytes("UTF-8"), "UTF-8"); |
| 523 | } catch (UnsupportedEncodingException e) { |
| 524 | Log.e(TAG, "err " + e); |
| 525 | } |
| 526 | if (utf8Url != null && utf8Url.startsWith("rtsp://")) { |
| 527 | return true; |
| 528 | } |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | private boolean handleRtspTypeUrl(String url) { |
| 533 | Intent intent; |
| 534 | // perform generic parsing of the URI to turn it into an Intent. |
| 535 | try { |
| 536 | intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
| 537 | } catch (URISyntaxException ex) { |
| 538 | Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage()); |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | try { |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 543 | getContext().startActivity(intent); |
kaiyiz | 950eca2 | 2013-08-08 11:01:28 +0800 | [diff] [blame] | 544 | } catch (ActivityNotFoundException ex) { |
| 545 | Log.w("Browser", "No resolveActivity " + url); |
| 546 | return false; |
| 547 | } |
| 548 | return true; |
| 549 | } |
| 550 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 551 | @Override |
| 552 | public void onDismiss() { |
| 553 | final Tab currentTab = mBaseUi.getActiveTab(); |
| 554 | mBaseUi.hideTitleBar(); |
| 555 | post(new Runnable() { |
| 556 | public void run() { |
| 557 | clearFocus(); |
Michael Kolb | 5ff5c8b | 2012-05-03 11:37:58 -0700 | [diff] [blame] | 558 | if (currentTab != null) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 559 | setDisplayTitle(currentTab.getUrl()); |
| 560 | } |
| 561 | } |
| 562 | }); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * callback from the suggestion dropdown |
| 567 | * copy text to input field and stay in edit mode |
| 568 | */ |
| 569 | @Override |
| 570 | public void onCopySuggestion(String text) { |
| 571 | mUrlInput.setText(text, true); |
| 572 | if (text != null) { |
| 573 | mUrlInput.setSelection(text.length()); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | public void setCurrentUrlIsBookmark(boolean isBookmark) { |
| 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public boolean dispatchKeyEventPreIme(KeyEvent evt) { |
| 582 | if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) { |
| 583 | // catch back key in order to do slightly more cleanup than usual |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 584 | stopEditingUrl(); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 585 | } |
| 586 | return super.dispatchKeyEventPreIme(evt); |
| 587 | } |
| 588 | |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 589 | /** |
| 590 | * called from the Ui when the user wants to edit |
| 591 | * @param clearInput clear the input field |
| 592 | */ |
Michael Kolb | 1f9b356 | 2012-04-24 14:38:34 -0700 | [diff] [blame] | 593 | void startEditingUrl(boolean clearInput, boolean forceIME) { |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 594 | // editing takes preference of progress |
| 595 | setVisibility(View.VISIBLE); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 596 | if (!mUrlInput.hasFocus()) { |
| 597 | mUrlInput.requestFocus(); |
| 598 | } |
| 599 | if (clearInput) { |
| 600 | mUrlInput.setText(""); |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 601 | } |
Michael Kolb | 1f9b356 | 2012-04-24 14:38:34 -0700 | [diff] [blame] | 602 | if (forceIME) { |
Michael Kolb | 4bb6fcb | 2012-04-13 14:25:27 -0700 | [diff] [blame] | 603 | mUrlInput.showIME(); |
| 604 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | public void onProgressStarted() { |
Pankaj Garg | 32e1b94 | 2015-06-03 18:13:24 -0700 | [diff] [blame] | 608 | mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_normal); |
| 609 | mSecurityState = Tab.SecurityState.SECURITY_STATE_NOT_SECURE; |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | public void onProgressStopped() { |
| 613 | } |
| 614 | |
John Reck | 419f6b4 | 2011-08-16 16:10:51 -0700 | [diff] [blame] | 615 | public void onTabDataChanged(Tab tab) { |
| 616 | } |
| 617 | |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 618 | public void onVoiceResult(String s) { |
| 619 | startEditingUrl(true, true); |
| 620 | onCopySuggestion(s); |
| 621 | } |
| 622 | |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 623 | @Override |
| 624 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { } |
| 625 | |
| 626 | @Override |
Michael Kolb | 5ff5c8b | 2012-05-03 11:37:58 -0700 | [diff] [blame] | 627 | public void onTextChanged(CharSequence s, int start, int before, int count) { } |
Narayan Kamath | f3174a5 | 2011-11-17 14:43:32 +0000 | [diff] [blame] | 628 | |
| 629 | @Override |
| 630 | public void afterTextChanged(Editable s) { } |
Michael Kolb | 0b12912 | 2012-06-04 16:31:58 -0700 | [diff] [blame] | 631 | |
Enrico Ros | 1f5a095 | 2014-11-18 20:15:48 -0800 | [diff] [blame] | 632 | @Override |
| 633 | public void onStateChanged(int state) { |
| 634 | switch(state) { |
| 635 | case STATE_NORMAL: |
| 636 | mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE); |
| 637 | break; |
| 638 | case STATE_HIGHLIGHTED: |
| 639 | mMore.setVisibility(View.GONE); |
| 640 | break; |
| 641 | case STATE_EDITED: |
| 642 | mMore.setVisibility(View.GONE); |
| 643 | break; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | public boolean isMenuShowing() { |
| 648 | return mOverflowMenuShowing; |
| 649 | } |
| 650 | |
| 651 | |
| 652 | @Override |
| 653 | public void onDismiss(PopupMenu popupMenu) { |
| 654 | if (popupMenu == mPopupMenu) { |
| 655 | onMenuHidden(); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | private void onMenuHidden() { |
| 660 | mOverflowMenuShowing = false; |
| 661 | mBaseUi.showTitleBarForDuration(); |
| 662 | } |
| 663 | |
| 664 | |
| 665 | @Override |
| 666 | public boolean onMenuItemClick(MenuItem item) { |
| 667 | return mUiController.onOptionsItemSelected(item); |
| 668 | } |
John Reck | 0f602f3 | 2011-07-07 15:38:43 -0700 | [diff] [blame] | 669 | } |