blob: c8984cfcaea3a10a747d04c643443cee68a98330 [file] [log] [blame]
John Reck0f602f32011-07-07 15:38:43 -07001/*
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 Amirzada41242f22014-03-21 12:12:18 -070016package com.android.browser;
John Reck0f602f32011-07-07 15:38:43 -070017
Pankaj Garg32e1b942015-06-03 18:13:24 -070018import android.animation.ArgbEvaluator;
19import android.animation.ValueAnimator;
Enrico Ros1f5a0952014-11-18 20:15:48 -080020import android.app.Activity;
John Reck0f602f32011-07-07 15:38:43 -070021import android.app.SearchManager;
kaiyizc4ada322013-07-30 09:58:07 +080022import android.content.ActivityNotFoundException;
John Reck0f602f32011-07-07 15:38:43 -070023import android.content.Context;
24import android.content.Intent;
Pankaj Garg32e1b942015-06-03 18:13:24 -070025import android.content.SharedPreferences;
John Reck0f602f32011-07-07 15:38:43 -070026import android.graphics.Bitmap;
Pankaj Garg66f8cef2015-07-07 17:29:03 -070027import android.graphics.BitmapFactory;
Pankaj Garg32e1b942015-06-03 18:13:24 -070028import android.graphics.Color;
kaiyizc4ada322013-07-30 09:58:07 +080029import android.net.Uri;
Pankaj Garg32e1b942015-06-03 18:13:24 -070030import android.net.http.SslCertificate;
31import android.net.http.SslError;
32import android.os.Build;
John Reck0f602f32011-07-07 15:38:43 -070033import android.os.Bundle;
Pankaj Garg66f8cef2015-07-07 17:29:03 -070034import android.os.Handler;
35import android.os.Message;
Narayan Kamathf3174a52011-11-17 14:43:32 +000036import android.text.Editable;
Pankaj Garg66f8cef2015-07-07 17:29:03 -070037import android.text.TextUtils;
Narayan Kamathf3174a52011-11-17 14:43:32 +000038import android.text.TextWatcher;
John Reck0f602f32011-07-07 15:38:43 -070039import android.util.AttributeSet;
kaiyizc4ada322013-07-30 09:58:07 +080040import android.util.Log;
John Reck0f602f32011-07-07 15:38:43 -070041import android.view.KeyEvent;
Enrico Ros1f5a0952014-11-18 20:15:48 -080042import android.view.Menu;
43import android.view.MenuItem;
John Reck0f602f32011-07-07 15:38:43 -070044import android.view.View;
45import android.view.View.OnClickListener;
46import android.view.View.OnFocusChangeListener;
Enrico Ros1f5a0952014-11-18 20:15:48 -080047import android.view.ViewConfiguration;
John Reck0f602f32011-07-07 15:38:43 -070048import android.widget.ImageView;
49import android.widget.LinearLayout;
Enrico Ros1f5a0952014-11-18 20:15:48 -080050import android.widget.PopupMenu;
kaiyizc4ada322013-07-30 09:58:07 +080051import android.widget.Toast;
John Reck0f602f32011-07-07 15:38:43 -070052
Bijan Amirzada41242f22014-03-21 12:12:18 -070053import com.android.browser.UrlInputView.UrlInputListener;
Pankaj Garg32e1b942015-06-03 18:13:24 -070054import com.android.browser.preferences.SiteSpecificPreferencesFragment;
John Reck0f602f32011-07-07 15:38:43 -070055
Pankaj Garg32e1b942015-06-03 18:13:24 -070056import java.io.ByteArrayOutputStream;
kaiyizc4ada322013-07-30 09:58:07 +080057import java.io.UnsupportedEncodingException;
Pankaj Garg32e1b942015-06-03 18:13:24 -070058import java.net.MalformedURLException;
kaiyiz950eca22013-08-08 11:01:28 +080059import java.net.URISyntaxException;
Pankaj Garg32e1b942015-06-03 18:13:24 -070060import java.net.URL;
kaiyizc4ada322013-07-30 09:58:07 +080061
Dany Rybnikovbcd37da2015-04-20 11:43:15 +030062import org.codeaurora.net.NetworkServices;
Kevin Hart055e6d82014-12-19 15:53:19 -080063import org.codeaurora.swe.Engine;
Pankaj Garg32e1b942015-06-03 18:13:24 -070064import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080065import org.codeaurora.swe.WebView;
Pankaj Garg32e1b942015-06-03 18:13:24 -070066import org.codeaurora.swe.util.ColorUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080067
John Reck42229bc2011-08-19 13:26:43 -070068public class NavigationBarBase extends LinearLayout implements
69 OnClickListener, UrlInputListener, OnFocusChangeListener,
Enrico Ros1f5a0952014-11-18 20:15:48 -080070 TextWatcher, UrlInputView.StateListener,
71 PopupMenu.OnMenuItemClickListener, PopupMenu.OnDismissListener {
John Reck0f602f32011-07-07 15:38:43 -070072
kaiyizc4ada322013-07-30 09:58:07 +080073 private final static String TAG = "NavigationBarBase";
74
John Reck0f602f32011-07-07 15:38:43 -070075 protected BaseUi mBaseUi;
76 protected TitleBar mTitleBar;
77 protected UiController mUiController;
78 protected UrlInputView mUrlInput;
Pankaj Garg66f8cef2015-07-07 17:29:03 -070079 protected ImageView mStopButton;
John Reck0f602f32011-07-07 15:38:43 -070080
Pankaj Garg66f8cef2015-07-07 17:29:03 -070081 private SiteTileView mFaviconTile;
82 private ImageView mMagnify;
83 private View mVoiceButton;
84 private ImageView mClearButton;
Enrico Ros1f5a0952014-11-18 20:15:48 -080085 private View mMore;
86 private PopupMenu mPopupMenu;
87 private boolean mOverflowMenuShowing;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070088
Pankaj Garg66f8cef2015-07-07 17:29:03 -070089 private static Bitmap mDefaultFavicon;
90
Pankaj Garg32e1b942015-06-03 18:13:24 -070091 private int mStatusBarColor;
92 private static int mDefaultStatusBarColor = -1;
93
Pankaj Garg66f8cef2015-07-07 17:29:03 -070094 private static final int WEBREFINER_COUNTER_MSG = 4242;
95 private static final int WEBREFINER_COUNTER_MSG_DELAY = 3000;
96 private Handler mHandler;
97
Pankaj Garg32e1b942015-06-03 18:13:24 -070098 private Tab.SecurityState mSecurityState = Tab.SecurityState.SECURITY_STATE_NOT_SECURE;
99
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700100 private static final String noSitePrefs[] = {
101 "chrome://",
102 "about:",
103 "content:",
104 };
105
John Reck0f602f32011-07-07 15:38:43 -0700106 public NavigationBarBase(Context context) {
107 super(context);
108 }
109
110 public NavigationBarBase(Context context, AttributeSet attrs) {
111 super(context, attrs);
112 }
113
114 public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) {
115 super(context, attrs, defStyle);
116 }
117
118 @Override
119 protected void onFinishInflate() {
120 super.onFinishInflate();
John Reck0f602f32011-07-07 15:38:43 -0700121 mUrlInput = (UrlInputView) findViewById(R.id.url);
122 mUrlInput.setUrlInputListener(this);
John Reck0f602f32011-07-07 15:38:43 -0700123 mUrlInput.setOnFocusChangeListener(this);
124 mUrlInput.setSelectAllOnFocus(true);
Narayan Kamathf3174a52011-11-17 14:43:32 +0000125 mUrlInput.addTextChangedListener(this);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800126 mMore = findViewById(R.id.more_browser_settings);
127 mMore.setOnClickListener(this);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700128 mMagnify = (ImageView) findViewById(R.id.magnify);
129 mMagnify.setOnClickListener(this);
130 mFaviconTile = (SiteTileView) findViewById(R.id.favicon_view);
131 mFaviconTile.setOnClickListener(this);
132 mVoiceButton = findViewById(R.id.voice);
133 mVoiceButton.setOnClickListener(this);
134 mClearButton = (ImageView) findViewById(R.id.clear);
135 mClearButton.setOnClickListener(this);
136 mStopButton = (ImageView) findViewById(R.id.stop);
137 mStopButton.setOnClickListener(this);
138
139 mDefaultFavicon = BitmapFactory.decodeResource(getResources(),
140 R.drawable.ic_deco_favicon_normal);
141
142 mHandler = new Handler() {
143 @Override
144 public void handleMessage(Message m) {
145 switch (m.what) {
146 case WEBREFINER_COUNTER_MSG:
147 WebView wv = mUiController.getCurrentTopWebView();
148 if (wv != null && WebRefiner.isInitialized()) {
149 int count = WebRefiner.getInstance().getBlockedURLCount(wv);
150 if (count > 0) {
151 mFaviconTile.setExtraBlockedObjectsCount(count);
152 }
153 }
154 mHandler.sendEmptyMessageDelayed(WEBREFINER_COUNTER_MSG,
155 WEBREFINER_COUNTER_MSG_DELAY);
156 break;
157 }
158 }
159 };
John Reck0f602f32011-07-07 15:38:43 -0700160 }
161
162 public void setTitleBar(TitleBar titleBar) {
163 mTitleBar = titleBar;
164 mBaseUi = mTitleBar.getUi();
165 mUiController = mTitleBar.getUiController();
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700166 mUrlInput.setController(mUiController);
John Reck0f602f32011-07-07 15:38:43 -0700167 }
168
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700169 public void setSecurityState(Tab.SecurityState securityState) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700170 mSecurityState = securityState;
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700171 switch (mSecurityState) {
172 case SECURITY_STATE_SECURE:
173 mFaviconTile.setTrustLevel(SiteTileView.TRUST_TRUSTED);
174 break;
175 case SECURITY_STATE_MIXED:
176 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNTRUSTED);
177 break;
178 case SECURITY_STATE_BAD_CERTIFICATE:
179 mFaviconTile.setTrustLevel(SiteTileView.TRUST_AVOID);
180 break;
181 case SECURITY_STATE_NOT_SECURE:
182 default:
183 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNKNOWN);
John Reck0f602f32011-07-07 15:38:43 -0700184 }
185 }
186
Pankaj Garg32e1b942015-06-03 18:13:24 -0700187 public static int adjustColor(int color, float hueMultiplier,
188 float saturationMultiplier, float valueMultiplier) {
189 float[] hsv = new float[3];
190 Color.colorToHSV(color, hsv);
191 hsv[0] *= hueMultiplier;
192 hsv[1] *= saturationMultiplier;
193 hsv[2] *= valueMultiplier;
194 return Color.HSVToColor(Color.alpha(color), hsv);
195 }
196
197 public static void setStatusAndNavigationBarColor(final Activity activity, int color) {
198 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
199 int currentColor = activity.getWindow().getStatusBarColor();
200 Integer from = currentColor;
201 Integer to = color;
202 ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
203
204 if (mDefaultStatusBarColor == -1) {
205 mDefaultStatusBarColor = activity.getWindow().getStatusBarColor();
206 }
207
208 animator.addUpdateListener(
209 new ValueAnimator.AnimatorUpdateListener() {
210 @Override
211 public void onAnimationUpdate(ValueAnimator animation) {
212 Integer value = (Integer) animation.getAnimatedValue();
213 activity.getWindow().setStatusBarColor(value.intValue());
214 //activity.getWindow().setNavigationBarColor(value.intValue());
215 }
216 }
217 );
218 animator.start();
219 }
220 }
221
222 private void updateSiteIconColor(String urlString, int color) {
223 try {
224 URL url = new URL(urlString);
225 String host = url.getHost();
226 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
227 int currentColor = prefs.getInt(host + ":color", 0);
228 if (currentColor != color) {
229 SharedPreferences.Editor editor = prefs.edit();
230 editor.remove(host + ":color");
231 editor.putInt(host + ":color", color);
232 editor.commit();
233 }
234 } catch (MalformedURLException e) {
235 }
236 }
237
238 public static int getSiteIconColor(String urlString) {
239 try {
240 URL url = new URL(urlString);
241 String host = url.getHost();
242 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
243 return prefs.getInt(host + ":color", 0);
244 } catch (MalformedURLException e) {
245 return 0;
246 }
247 }
248
249 public static int getDefaultStatusBarColor() {
250 return mDefaultStatusBarColor;
251 }
252
John Reck0f602f32011-07-07 15:38:43 -0700253 public void setFavicon(Bitmap icon) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700254 int color = ColorUtils.getDominantColorForBitmap(icon);
255 Tab tab = mUiController.getCurrentTab();
256
257 if (tab != null) {
258 if (tab.hasFavicon()) {
259 updateSiteIconColor(tab.getUrl(), color);
260 setStatusAndNavigationBarColor(mUiController.getActivity(),
261 adjustColor(color, 1, 1, 0.7f));
262 } else {
263 color = getSiteIconColor(tab.getUrl());
264 if (color != 0) {
265 setStatusAndNavigationBarColor(mUiController.getActivity(),
266 adjustColor(color, 1, 1, 0.7f));
267 } else {
268 setStatusAndNavigationBarColor(mUiController.getActivity(),
269 mDefaultStatusBarColor);
270 }
271 }
272 } else {
273 setStatusAndNavigationBarColor(mUiController.getActivity(), mDefaultStatusBarColor);
274 }
275
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700276 //Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
277
278 if (mFaviconTile != null) {
279 mFaviconTile.replaceFavicon(mUiController.getCurrentTopWebView().getFavicon());
280 }
John Reck0f602f32011-07-07 15:38:43 -0700281 }
282
Pankaj Garg32e1b942015-06-03 18:13:24 -0700283 protected void showSiteSpecificSettings() {
284 WebView wv = mUiController.getCurrentTopWebView();
285 int count = 0;
286
287 if (wv != null && WebRefiner.getInstance() != null) {
288 count = WebRefiner.getInstance().getBlockedURLCount(wv);
289 }
290
291 Bundle bundle = new Bundle();
292 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE,
293 mUiController.getCurrentTab().getUrl());
294 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_INFO, count);
295
296 bundle.putParcelable(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT,
297 SslCertificate.saveState(wv.getCertificate()));
298
299 SslError error = mUiController.getCurrentTab().getSslCertificateError();
300 if (error != null) {
301 int certError = 0;
302 if (error.hasError(SslError.SSL_DATE_INVALID)) {
303 certError |= (1 << SslError.SSL_DATE_INVALID);
304 }
305
306 if (error.hasError(SslError.SSL_EXPIRED)) {
307 certError |= (1 << SslError.SSL_EXPIRED);
308 }
309
310 if (error.hasError(SslError.SSL_IDMISMATCH)) {
311 certError |= (1 << SslError.SSL_IDMISMATCH);
312 }
313
314 if (error.hasError(SslError.SSL_INVALID)) {
315 certError |= (1 << SslError.SSL_INVALID);
316 }
317
318 if (error.hasError(SslError.SSL_NOTYETVALID)) {
319 certError |= (1 << SslError.SSL_NOTYETVALID);
320 }
321
322 if (error.hasError(SslError.SSL_UNTRUSTED)) {
323 certError |= (1 << SslError.SSL_UNTRUSTED);
324 }
325
326 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_ERR, certError);
327 }
328
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700329 Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
Pankaj Garg32e1b942015-06-03 18:13:24 -0700330 if (favicon != null) {
331 ByteArrayOutputStream baos = new ByteArrayOutputStream();
332 favicon.compress(Bitmap.CompressFormat.PNG, 50, baos);
333 bundle.putByteArray(SiteSpecificPreferencesFragment.EXTRA_FAVICON,
334 baos.toByteArray());
335 }
336 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(
337 mUiController.getActivity(),
338 SiteSpecificPreferencesFragment.class.getName(), bundle, 0);
339 }
340
John Reck0f602f32011-07-07 15:38:43 -0700341 @Override
342 public void onClick(View v) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700343 Tab currentTab = mUiController.getCurrentTab();
344 String url = null;
345 if (currentTab != null){
346 url = currentTab.getUrl();
347 }
Enrico Ros1f5a0952014-11-18 20:15:48 -0800348 if (mMore == v) {
349 showMenu(mMore);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700350 } else if (mFaviconTile == v) {
351 if (urlHasSitePrefs(url)){
352 showSiteSpecificSettings();
353 }
354 } else if (mMagnify == v) {
355 startEditingUrl(true, true);
356 } else if (mVoiceButton == v) {
357 mUiController.startVoiceRecognizer();
358 } else if (mStopButton == v) {
359 stopOrRefresh();
360 } else if (mClearButton == v) {
361 clearOrClose();
362 mUrlInput.setText("");
363 }
364 }
365
366 private static boolean urlHasSitePrefs(String url) {
367 if (TextUtils.isEmpty(url)) {
368 return false;
369 }
370
371 for (int i = 0; i < noSitePrefs.length; i++) {
372 if (url.startsWith(noSitePrefs[i])) {
373 return false;
374 }
375 }
376 return true;
377 }
378
379 private void stopOrRefresh() {
380 if (mUiController == null) return;
381 if (mTitleBar.isInLoad()) {
382 mUiController.stopLoading();
383 } else {
384 if (mUiController.getCurrentTopWebView() != null) {
385 stopEditingUrl();
386 mUiController.getCurrentTopWebView().reload();
387 }
388 }
389 }
390
391 private void clearOrClose() {
392 if (TextUtils.isEmpty(mUrlInput.getText())) {
393 // close
394 mUrlInput.clearFocus();
395 } else {
396 // clear
397 mUrlInput.setText("");
Enrico Ros1f5a0952014-11-18 20:15:48 -0800398 }
399 }
400
401 void showMenu(View anchor) {
402 Activity activity = mUiController.getActivity();
403 if (mPopupMenu == null) {
404 mPopupMenu = new PopupMenu(getContext(), anchor);
405 mPopupMenu.setOnMenuItemClickListener(this);
406 mPopupMenu.setOnDismissListener(this);
407 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
408 mPopupMenu = null;
409 return;
410 }
411 }
412 Menu menu = mPopupMenu.getMenu();
413
414 if (mUiController instanceof Controller) {
415 Controller controller = (Controller) mUiController;
416 if (controller.onPrepareOptionsMenu(menu)) {
417 mOverflowMenuShowing = true;
418 }
419 }
John Reck0f602f32011-07-07 15:38:43 -0700420 }
421
422 @Override
423 public void onFocusChange(View view, boolean hasFocus) {
424 // if losing focus and not in touch mode, leave as is
425 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
426 setFocusState(hasFocus);
427 }
428 if (hasFocus) {
Kevin Hart055e6d82014-12-19 15:53:19 -0800429 Engine.warmUpChildProcessAsync(mUiController.getActivity().getApplicationContext());
John Reck0f602f32011-07-07 15:38:43 -0700430 mBaseUi.showTitleBar();
Dany Rybnikovbcd37da2015-04-20 11:43:15 +0300431 if (!BrowserSettings.getInstance().isPowerSaveModeEnabled()) {
432 //Notify about anticipated network activity
433 NetworkServices.hintUpcomingUserActivity();
434 }
John Reck0f602f32011-07-07 15:38:43 -0700435 } else if (!mUrlInput.needsUpdate()) {
436 mUrlInput.dismissDropDown();
437 mUrlInput.hideIME();
438 if (mUrlInput.getText().length() == 0) {
439 Tab currentTab = mUiController.getTabControl().getCurrentTab();
440 if (currentTab != null) {
John Reck434e9f82011-08-10 18:16:52 -0700441 setDisplayTitle(currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700442 }
443 }
444 mBaseUi.suggestHideTitleBar();
445 }
446 mUrlInput.clearNeedsUpdate();
447 }
448
449 protected void setFocusState(boolean focus) {
450 }
451
John Reck0f602f32011-07-07 15:38:43 -0700452 public boolean isEditingUrl() {
453 return mUrlInput.hasFocus();
454 }
455
456 void stopEditingUrl() {
Michael Kolb0b129122012-06-04 16:31:58 -0700457 WebView currentTopWebView = mUiController.getCurrentTopWebView();
458 if (currentTopWebView != null) {
459 currentTopWebView.requestFocus();
460 }
John Reck0f602f32011-07-07 15:38:43 -0700461 }
462
463 void setDisplayTitle(String title) {
464 if (!isEditingUrl()) {
Michael Kolb29aab402012-05-29 17:22:35 -0700465 if (!title.equals(mUrlInput.getText().toString())) {
466 mUrlInput.setText(title, false);
467 }
John Reck0f602f32011-07-07 15:38:43 -0700468 }
469 }
470
John Reck0f602f32011-07-07 15:38:43 -0700471 void setIncognitoMode(boolean incognito) {
472 mUrlInput.setIncognitoMode(incognito);
473 }
474
475 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000476 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700477 }
478
479 // UrlInputListener implementation
480
481 /**
482 * callback from suggestion dropdown
483 * user selected a suggestion
484 */
485 @Override
486 public void onAction(String text, String extra, String source) {
Michael Kolb0b129122012-06-04 16:31:58 -0700487 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700488 if (UrlInputView.TYPED.equals(source)) {
kaiyizc4ada322013-07-30 09:58:07 +0800489 String url = null;
Panos Thomas4bdb5252014-11-13 16:20:11 -0800490 boolean wap2estore = BrowserConfig.getInstance(getContext())
491 .hasFeature(BrowserConfig.Feature.WAP2ESTORE);
Vivek Sekhar5e631472014-03-31 23:59:10 -0700492 if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text)
493 || isMakeCallTypeUrl(text)) {
kaiyizc4ada322013-07-30 09:58:07 +0800494 url = text;
495 } else {
496 url = UrlUtils.smartUrlFilter(text, false);
497 }
498
John Reck0f602f32011-07-07 15:38:43 -0700499 Tab t = mBaseUi.getActiveTab();
500 // Only shortcut javascript URIs for now, as there is special
501 // logic in UrlHandler for other schemas
502 if (url != null && t != null && url.startsWith("javascript:")) {
503 mUiController.loadUrl(t, url);
504 setDisplayTitle(text);
505 return;
506 }
kaiyizc4ada322013-07-30 09:58:07 +0800507
508 // add for carrier wap2estore feature
509 if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700510 if (handleEstoreTypeUrl(url)) {
511 setDisplayTitle(text);
512 return;
513 }
kaiyizc4ada322013-07-30 09:58:07 +0800514 }
kaiyiz950eca22013-08-08 11:01:28 +0800515 // add for rtsp scheme feature
516 if (url != null && t != null && isRtspTypeUrl(url)) {
517 if (handleRtspTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700518 setDisplayTitle(text);
kaiyiz950eca22013-08-08 11:01:28 +0800519 return;
520 }
521 }
Vivek Sekhar5e631472014-03-31 23:59:10 -0700522 // add for "wtai://wp/mc;" scheme feature
523 if (url != null && t != null && isMakeCallTypeUrl(url)) {
524 if (handleMakeCallTypeUrl(url)) {
525 return;
526 }
527 }
John Reck0f602f32011-07-07 15:38:43 -0700528 }
529 Intent i = new Intent();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700530 String action = Intent.ACTION_SEARCH;
John Reck0f602f32011-07-07 15:38:43 -0700531 i.setAction(action);
532 i.putExtra(SearchManager.QUERY, text);
533 if (extra != null) {
534 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
535 }
536 if (source != null) {
537 Bundle appData = new Bundle();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800538 appData.putString("source", source);
539 i.putExtra("source", appData);
John Reck0f602f32011-07-07 15:38:43 -0700540 }
541 mUiController.handleNewIntent(i);
542 setDisplayTitle(text);
543 }
544
Vivek Sekhar5e631472014-03-31 23:59:10 -0700545 private boolean isMakeCallTypeUrl(String url) {
546 String utf8Url = null;
547 try {
548 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
549 } catch (UnsupportedEncodingException e) {
550 Log.e(TAG, "err " + e);
551 }
552 if (utf8Url != null && utf8Url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
553 return true;
554 }
555 return false;
556 }
557
558 private boolean handleMakeCallTypeUrl(String url) {
559 // wtai://wp/mc;number
560 // number=string(phone-number)
561 if (url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
562 Intent intent = new Intent(Intent.ACTION_VIEW,
563 Uri.parse(WebView.SCHEME_TEL +
564 url.substring(UrlHandler.SCHEME_WTAI_MC.length())));
565 getContext().startActivity(intent);
566 // before leaving BrowserActivity, close the empty child tab.
567 // If a new tab is created through JavaScript open to load this
568 // url, we would like to close it as we will load this url in a
569 // different Activity.
570 Tab current = mUiController.getCurrentTab();
571 if (current != null
572 && current.getWebView().copyBackForwardList().getSize() == 0) {
573 mUiController.closeCurrentTab();
574 }
575 return true;
576 }
577 return false;
578 }
579
kaiyizc4ada322013-07-30 09:58:07 +0800580 private boolean isEstoreTypeUrl(String url) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700581 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800582 return true;
583 }
584 return false;
585 }
586
Vivek Sekharb54614f2014-05-01 19:03:37 -0700587 private boolean handleEstoreTypeUrl(String url) {
588 if (url.getBytes().length > 256) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800589 Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700590 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800591 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700592
593 Intent intent;
594 // perform generic parsing of the URI to turn it into an Intent.
595 try {
596 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
597 } catch (URISyntaxException ex) {
598 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
599 return false;
600 }
601
kaiyizc4ada322013-07-30 09:58:07 +0800602 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800603 getContext().startActivity(intent);
kaiyizc4ada322013-07-30 09:58:07 +0800604 } catch (ActivityNotFoundException ex) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800605 String downloadUrl = getContext().getResources().getString(R.string.estore_homepage);
kaiyizc4ada322013-07-30 09:58:07 +0800606 mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800607 Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800608 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700609
610 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800611 }
612
kaiyiz950eca22013-08-08 11:01:28 +0800613 private boolean isRtspTypeUrl(String url) {
614 String utf8Url = null;
615 try {
616 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
617 } catch (UnsupportedEncodingException e) {
618 Log.e(TAG, "err " + e);
619 }
620 if (utf8Url != null && utf8Url.startsWith("rtsp://")) {
621 return true;
622 }
623 return false;
624 }
625
626 private boolean handleRtspTypeUrl(String url) {
627 Intent intent;
628 // perform generic parsing of the URI to turn it into an Intent.
629 try {
630 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
631 } catch (URISyntaxException ex) {
632 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
633 return false;
634 }
635
636 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800637 getContext().startActivity(intent);
kaiyiz950eca22013-08-08 11:01:28 +0800638 } catch (ActivityNotFoundException ex) {
639 Log.w("Browser", "No resolveActivity " + url);
640 return false;
641 }
642 return true;
643 }
644
John Reck0f602f32011-07-07 15:38:43 -0700645 @Override
646 public void onDismiss() {
647 final Tab currentTab = mBaseUi.getActiveTab();
648 mBaseUi.hideTitleBar();
649 post(new Runnable() {
650 public void run() {
651 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700652 if (currentTab != null) {
John Reck0f602f32011-07-07 15:38:43 -0700653 setDisplayTitle(currentTab.getUrl());
654 }
655 }
656 });
657 }
658
659 /**
660 * callback from the suggestion dropdown
661 * copy text to input field and stay in edit mode
662 */
663 @Override
664 public void onCopySuggestion(String text) {
665 mUrlInput.setText(text, true);
666 if (text != null) {
667 mUrlInput.setSelection(text.length());
668 }
669 }
670
671 public void setCurrentUrlIsBookmark(boolean isBookmark) {
672 }
673
674 @Override
675 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
676 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
677 // catch back key in order to do slightly more cleanup than usual
Michael Kolb0b129122012-06-04 16:31:58 -0700678 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700679 }
680 return super.dispatchKeyEventPreIme(evt);
681 }
682
John Reck0f602f32011-07-07 15:38:43 -0700683 /**
684 * called from the Ui when the user wants to edit
685 * @param clearInput clear the input field
686 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700687 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700688 // editing takes preference of progress
689 setVisibility(View.VISIBLE);
John Reck0f602f32011-07-07 15:38:43 -0700690 if (!mUrlInput.hasFocus()) {
691 mUrlInput.requestFocus();
692 }
693 if (clearInput) {
694 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700695 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700696 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700697 mUrlInput.showIME();
698 }
John Reck0f602f32011-07-07 15:38:43 -0700699 }
700
701 public void onProgressStarted() {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700702 mFaviconTile.setExtraBlockedObjectsCount(0);
703 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNKNOWN);
704 mFaviconTile.replaceFavicon(mDefaultFavicon);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700705 mSecurityState = Tab.SecurityState.SECURITY_STATE_NOT_SECURE;
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700706 mHandler.removeMessages(WEBREFINER_COUNTER_MSG);
707 mHandler.sendEmptyMessageDelayed(WEBREFINER_COUNTER_MSG,
708 WEBREFINER_COUNTER_MSG_DELAY);
709 mStopButton.setImageResource(R.drawable.ic_action_stop);
710 mStopButton.setContentDescription(getResources().
711 getString(R.string.accessibility_button_stop));
John Reck0f602f32011-07-07 15:38:43 -0700712 }
713
714 public void onProgressStopped() {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700715 if (!isEditingUrl()) {
716 mFaviconTile.setVisibility(View.VISIBLE);
717 }
718 mStopButton.setImageResource(R.drawable.ic_action_reload);
719 mStopButton.setContentDescription(getResources().
720 getString(R.string.accessibility_button_refresh));
John Reck0f602f32011-07-07 15:38:43 -0700721 }
722
John Reck419f6b42011-08-16 16:10:51 -0700723 public void onTabDataChanged(Tab tab) {
724 }
725
Michael Kolb0b129122012-06-04 16:31:58 -0700726 public void onVoiceResult(String s) {
727 startEditingUrl(true, true);
728 onCopySuggestion(s);
729 }
730
Narayan Kamathf3174a52011-11-17 14:43:32 +0000731 @Override
732 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
733
734 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700735 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000736
737 @Override
738 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700739
Enrico Ros1f5a0952014-11-18 20:15:48 -0800740 @Override
741 public void onStateChanged(int state) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700742 mVoiceButton.setVisibility(View.GONE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800743 switch(state) {
744 case STATE_NORMAL:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700745 mFaviconTile.setVisibility(View.VISIBLE);
746 mMagnify.setVisibility(View.GONE);
747 mClearButton.setVisibility(View.GONE);
Sagar Dhawanaff56c92015-07-13 15:57:11 -0700748 mMore.setVisibility(View.VISIBLE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700749 if (mUiController != null) {
750 Tab currentTab = mUiController.getCurrentTab();
751 if (currentTab != null){
752 if (TextUtils.isEmpty(currentTab.getUrl())) {
753 mFaviconTile.setVisibility(View.GONE);
754 mMagnify.setVisibility(View.VISIBLE);
755 }
756 }
757 mUiController.setWindowDimming(0.0f);
758 }
759
Enrico Ros1f5a0952014-11-18 20:15:48 -0800760 break;
761 case STATE_HIGHLIGHTED:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700762 mFaviconTile.setVisibility(View.GONE);
763 mMagnify.setVisibility(View.GONE);
764 mClearButton.setVisibility(View.GONE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800765 mMore.setVisibility(View.GONE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700766 if (mUiController != null) {
767 mUiController.setWindowDimming(0.75f);
768 if (mUiController.supportsVoice()) {
769 mVoiceButton.setVisibility(View.VISIBLE);
770 }
771 }
Enrico Ros1f5a0952014-11-18 20:15:48 -0800772 break;
773 case STATE_EDITED:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700774 mFaviconTile.setVisibility(View.GONE);
775 mMagnify.setVisibility(View.VISIBLE);
776 mClearButton.setVisibility(View.VISIBLE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800777 mMore.setVisibility(View.GONE);
778 break;
779 }
780 }
781
782 public boolean isMenuShowing() {
783 return mOverflowMenuShowing;
784 }
785
786
787 @Override
788 public void onDismiss(PopupMenu popupMenu) {
789 if (popupMenu == mPopupMenu) {
790 onMenuHidden();
791 }
792 }
793
794 private void onMenuHidden() {
795 mOverflowMenuShowing = false;
796 mBaseUi.showTitleBarForDuration();
797 }
798
799
800 @Override
801 public boolean onMenuItemClick(MenuItem item) {
802 return mUiController.onOptionsItemSelected(item);
803 }
John Reck0f602f32011-07-07 15:38:43 -0700804}