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