blob: c1ad5c90bd0481c77dc59f8ef7b8481b15a7eee5 [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;
John Reck0f602f32011-07-07 15:38:43 -070047import android.widget.ImageView;
48import android.widget.LinearLayout;
Enrico Ros1f5a0952014-11-18 20:15:48 -080049import android.widget.PopupMenu;
kaiyizc4ada322013-07-30 09:58:07 +080050import android.widget.Toast;
John Reck0f602f32011-07-07 15:38:43 -070051
Bijan Amirzada41242f22014-03-21 12:12:18 -070052import com.android.browser.UrlInputView.UrlInputListener;
Pankaj Garg32e1b942015-06-03 18:13:24 -070053import com.android.browser.preferences.SiteSpecificPreferencesFragment;
John Reck0f602f32011-07-07 15:38:43 -070054
Pankaj Garg32e1b942015-06-03 18:13:24 -070055import java.io.ByteArrayOutputStream;
kaiyizc4ada322013-07-30 09:58:07 +080056import java.io.UnsupportedEncodingException;
Pankaj Garg32e1b942015-06-03 18:13:24 -070057import java.net.MalformedURLException;
kaiyiz950eca22013-08-08 11:01:28 +080058import java.net.URISyntaxException;
Pankaj Garg32e1b942015-06-03 18:13:24 -070059import java.net.URL;
kaiyizc4ada322013-07-30 09:58:07 +080060
Dany Rybnikovbcd37da2015-04-20 11:43:15 +030061import org.codeaurora.net.NetworkServices;
Kevin Hart055e6d82014-12-19 15:53:19 -080062import org.codeaurora.swe.Engine;
Pankaj Garg32e1b942015-06-03 18:13:24 -070063import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080064import org.codeaurora.swe.WebView;
Pankaj Garg32e1b942015-06-03 18:13:24 -070065import org.codeaurora.swe.util.ColorUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080066
John Reck42229bc2011-08-19 13:26:43 -070067public class NavigationBarBase extends LinearLayout implements
68 OnClickListener, UrlInputListener, OnFocusChangeListener,
Enrico Ros1f5a0952014-11-18 20:15:48 -080069 TextWatcher, UrlInputView.StateListener,
70 PopupMenu.OnMenuItemClickListener, PopupMenu.OnDismissListener {
John Reck0f602f32011-07-07 15:38:43 -070071
kaiyizc4ada322013-07-30 09:58:07 +080072 private final static String TAG = "NavigationBarBase";
73
John Reck0f602f32011-07-07 15:38:43 -070074 protected BaseUi mBaseUi;
75 protected TitleBar mTitleBar;
76 protected UiController mUiController;
77 protected UrlInputView mUrlInput;
Pankaj Garg66f8cef2015-07-07 17:29:03 -070078 protected ImageView mStopButton;
John Reck0f602f32011-07-07 15:38:43 -070079
Pankaj Garg66f8cef2015-07-07 17:29:03 -070080 private SiteTileView mFaviconTile;
81 private ImageView mMagnify;
82 private View mVoiceButton;
83 private ImageView mClearButton;
Enrico Ros1f5a0952014-11-18 20:15:48 -080084 private View mMore;
85 private PopupMenu mPopupMenu;
86 private boolean mOverflowMenuShowing;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070087
Pankaj Garg66f8cef2015-07-07 17:29:03 -070088 private static Bitmap mDefaultFavicon;
89
Pankaj Garg32e1b942015-06-03 18:13:24 -070090 private int mStatusBarColor;
91 private static int mDefaultStatusBarColor = -1;
92
Pankaj Garg66f8cef2015-07-07 17:29:03 -070093 private static final int WEBREFINER_COUNTER_MSG = 4242;
94 private static final int WEBREFINER_COUNTER_MSG_DELAY = 3000;
95 private Handler mHandler;
96
Pankaj Garg32e1b942015-06-03 18:13:24 -070097 private Tab.SecurityState mSecurityState = Tab.SecurityState.SECURITY_STATE_NOT_SECURE;
98
Pankaj Garg66f8cef2015-07-07 17:29:03 -070099 private static final String noSitePrefs[] = {
100 "chrome://",
101 "about:",
102 "content:",
103 };
104
John Reck0f602f32011-07-07 15:38:43 -0700105 public NavigationBarBase(Context context) {
106 super(context);
107 }
108
109 public NavigationBarBase(Context context, AttributeSet attrs) {
110 super(context, attrs);
111 }
112
113 public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) {
114 super(context, attrs, defStyle);
115 }
116
117 @Override
118 protected void onFinishInflate() {
119 super.onFinishInflate();
John Reck0f602f32011-07-07 15:38:43 -0700120 mUrlInput = (UrlInputView) findViewById(R.id.url);
121 mUrlInput.setUrlInputListener(this);
John Reck0f602f32011-07-07 15:38:43 -0700122 mUrlInput.setOnFocusChangeListener(this);
123 mUrlInput.setSelectAllOnFocus(true);
Narayan Kamathf3174a52011-11-17 14:43:32 +0000124 mUrlInput.addTextChangedListener(this);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800125 mMore = findViewById(R.id.more_browser_settings);
126 mMore.setOnClickListener(this);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700127 mMagnify = (ImageView) findViewById(R.id.magnify);
128 mMagnify.setOnClickListener(this);
129 mFaviconTile = (SiteTileView) findViewById(R.id.favicon_view);
130 mFaviconTile.setOnClickListener(this);
131 mVoiceButton = findViewById(R.id.voice);
132 mVoiceButton.setOnClickListener(this);
133 mClearButton = (ImageView) findViewById(R.id.clear);
134 mClearButton.setOnClickListener(this);
135 mStopButton = (ImageView) findViewById(R.id.stop);
136 mStopButton.setOnClickListener(this);
137
138 mDefaultFavicon = BitmapFactory.decodeResource(getResources(),
139 R.drawable.ic_deco_favicon_normal);
140
141 mHandler = new Handler() {
142 @Override
143 public void handleMessage(Message m) {
144 switch (m.what) {
145 case WEBREFINER_COUNTER_MSG:
146 WebView wv = mUiController.getCurrentTopWebView();
147 if (wv != null && WebRefiner.isInitialized()) {
148 int count = WebRefiner.getInstance().getBlockedURLCount(wv);
149 if (count > 0) {
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700150 mFaviconTile.setBadgeBlockedObjectsCount(count);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700151 }
152 }
153 mHandler.sendEmptyMessageDelayed(WEBREFINER_COUNTER_MSG,
154 WEBREFINER_COUNTER_MSG_DELAY);
155 break;
156 }
157 }
158 };
John Reck0f602f32011-07-07 15:38:43 -0700159 }
160
161 public void setTitleBar(TitleBar titleBar) {
162 mTitleBar = titleBar;
163 mBaseUi = mTitleBar.getUi();
164 mUiController = mTitleBar.getUiController();
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700165 mUrlInput.setController(mUiController);
John Reck0f602f32011-07-07 15:38:43 -0700166 }
167
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700168 public void setSecurityState(Tab.SecurityState securityState) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700169 mSecurityState = securityState;
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700170 switch (mSecurityState) {
171 case SECURITY_STATE_SECURE:
172 mFaviconTile.setTrustLevel(SiteTileView.TRUST_TRUSTED);
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700173 mFaviconTile.setBadgeHasCertIssues(false);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700174 break;
175 case SECURITY_STATE_MIXED:
176 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNTRUSTED);
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700177 mFaviconTile.setBadgeHasCertIssues(true);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700178 break;
179 case SECURITY_STATE_BAD_CERTIFICATE:
180 mFaviconTile.setTrustLevel(SiteTileView.TRUST_AVOID);
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700181 mFaviconTile.setBadgeHasCertIssues(true);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700182 break;
183 case SECURITY_STATE_NOT_SECURE:
184 default:
185 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNKNOWN);
John Reck0f602f32011-07-07 15:38:43 -0700186 }
187 }
188
Pankaj Garg32e1b942015-06-03 18:13:24 -0700189 public static int adjustColor(int color, float hueMultiplier,
190 float saturationMultiplier, float valueMultiplier) {
191 float[] hsv = new float[3];
192 Color.colorToHSV(color, hsv);
193 hsv[0] *= hueMultiplier;
194 hsv[1] *= saturationMultiplier;
195 hsv[2] *= valueMultiplier;
196 return Color.HSVToColor(Color.alpha(color), hsv);
197 }
198
199 public static void setStatusAndNavigationBarColor(final Activity activity, int color) {
200 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
201 int currentColor = activity.getWindow().getStatusBarColor();
202 Integer from = currentColor;
203 Integer to = color;
204 ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
205
206 if (mDefaultStatusBarColor == -1) {
207 mDefaultStatusBarColor = activity.getWindow().getStatusBarColor();
208 }
209
210 animator.addUpdateListener(
211 new ValueAnimator.AnimatorUpdateListener() {
212 @Override
213 public void onAnimationUpdate(ValueAnimator animation) {
214 Integer value = (Integer) animation.getAnimatedValue();
215 activity.getWindow().setStatusBarColor(value.intValue());
216 //activity.getWindow().setNavigationBarColor(value.intValue());
217 }
218 }
219 );
220 animator.start();
221 }
222 }
223
224 private void updateSiteIconColor(String urlString, int color) {
225 try {
226 URL url = new URL(urlString);
227 String host = url.getHost();
228 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
229 int currentColor = prefs.getInt(host + ":color", 0);
230 if (currentColor != color) {
231 SharedPreferences.Editor editor = prefs.edit();
232 editor.remove(host + ":color");
233 editor.putInt(host + ":color", color);
234 editor.commit();
235 }
236 } catch (MalformedURLException e) {
237 }
238 }
239
240 public static int getSiteIconColor(String urlString) {
241 try {
242 URL url = new URL(urlString);
243 String host = url.getHost();
244 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
245 return prefs.getInt(host + ":color", 0);
246 } catch (MalformedURLException e) {
247 return 0;
248 }
249 }
250
251 public static int getDefaultStatusBarColor() {
252 return mDefaultStatusBarColor;
253 }
254
John Reck0f602f32011-07-07 15:38:43 -0700255 public void setFavicon(Bitmap icon) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700256 int color = ColorUtils.getDominantColorForBitmap(icon);
257 Tab tab = mUiController.getCurrentTab();
258
259 if (tab != null) {
260 if (tab.hasFavicon()) {
261 updateSiteIconColor(tab.getUrl(), color);
262 setStatusAndNavigationBarColor(mUiController.getActivity(),
263 adjustColor(color, 1, 1, 0.7f));
264 } else {
265 color = getSiteIconColor(tab.getUrl());
266 if (color != 0) {
267 setStatusAndNavigationBarColor(mUiController.getActivity(),
268 adjustColor(color, 1, 1, 0.7f));
269 } else {
270 setStatusAndNavigationBarColor(mUiController.getActivity(),
271 mDefaultStatusBarColor);
272 }
273 }
274 } else {
275 setStatusAndNavigationBarColor(mUiController.getActivity(), mDefaultStatusBarColor);
276 }
277
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700278 //Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
279
280 if (mFaviconTile != null) {
281 mFaviconTile.replaceFavicon(mUiController.getCurrentTopWebView().getFavicon());
282 }
John Reck0f602f32011-07-07 15:38:43 -0700283 }
284
Pankaj Garg32e1b942015-06-03 18:13:24 -0700285 protected void showSiteSpecificSettings() {
286 WebView wv = mUiController.getCurrentTopWebView();
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700287 int ads = 0;
288 int tracker = 0;
289 int malware = 0;
Pankaj Garg32e1b942015-06-03 18:13:24 -0700290
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700291 WebRefiner webRefiner = WebRefiner.getInstance();
292 if (wv != null && webRefiner != null) {
293 WebRefiner.PageInfo pageInfo = webRefiner.getPageInfo(wv);
294 if (pageInfo != null) {
295 for (WebRefiner.MatchedURLInfo urlInfo : pageInfo.mMatchedURLInfoList) {
296 switch (urlInfo.mMatchedFilterCategory) {
297 case WebRefiner.RuleSet.CATEGORY_ADS:
298 ads++;
299 break;
300 case WebRefiner.RuleSet.CATEGORY_TRACKERS:
301 tracker++;
302 break;
303 case WebRefiner.RuleSet.CATEGORY_MALWARE_DOMAINS:
304 malware++;
305 break;
306 }
307 }
308 }
Pankaj Garg32e1b942015-06-03 18:13:24 -0700309 }
310
311 Bundle bundle = new Bundle();
312 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE,
313 mUiController.getCurrentTab().getUrl());
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700314
315 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_ADS_INFO, ads);
316 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_TRACKER_INFO, tracker);
317 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_MALWARE_INFO, malware);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700318
319 bundle.putParcelable(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT,
320 SslCertificate.saveState(wv.getCertificate()));
321
322 SslError error = mUiController.getCurrentTab().getSslCertificateError();
323 if (error != null) {
324 int certError = 0;
325 if (error.hasError(SslError.SSL_DATE_INVALID)) {
326 certError |= (1 << SslError.SSL_DATE_INVALID);
327 }
328
329 if (error.hasError(SslError.SSL_EXPIRED)) {
330 certError |= (1 << SslError.SSL_EXPIRED);
331 }
332
333 if (error.hasError(SslError.SSL_IDMISMATCH)) {
334 certError |= (1 << SslError.SSL_IDMISMATCH);
335 }
336
337 if (error.hasError(SslError.SSL_INVALID)) {
338 certError |= (1 << SslError.SSL_INVALID);
339 }
340
341 if (error.hasError(SslError.SSL_NOTYETVALID)) {
342 certError |= (1 << SslError.SSL_NOTYETVALID);
343 }
344
345 if (error.hasError(SslError.SSL_UNTRUSTED)) {
346 certError |= (1 << SslError.SSL_UNTRUSTED);
347 }
348
349 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_ERR, certError);
350 }
351
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700352 Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
Pankaj Garg32e1b942015-06-03 18:13:24 -0700353 if (favicon != null) {
354 ByteArrayOutputStream baos = new ByteArrayOutputStream();
355 favicon.compress(Bitmap.CompressFormat.PNG, 50, baos);
356 bundle.putByteArray(SiteSpecificPreferencesFragment.EXTRA_FAVICON,
357 baos.toByteArray());
358 }
359 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(
360 mUiController.getActivity(),
361 SiteSpecificPreferencesFragment.class.getName(), bundle, 0);
362 }
363
John Reck0f602f32011-07-07 15:38:43 -0700364 @Override
365 public void onClick(View v) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700366 Tab currentTab = mUiController.getCurrentTab();
367 String url = null;
368 if (currentTab != null){
369 url = currentTab.getUrl();
370 }
Enrico Ros1f5a0952014-11-18 20:15:48 -0800371 if (mMore == v) {
372 showMenu(mMore);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700373 } else if (mFaviconTile == v) {
374 if (urlHasSitePrefs(url)){
375 showSiteSpecificSettings();
376 }
377 } else if (mMagnify == v) {
378 startEditingUrl(true, true);
379 } else if (mVoiceButton == v) {
380 mUiController.startVoiceRecognizer();
381 } else if (mStopButton == v) {
382 stopOrRefresh();
383 } else if (mClearButton == v) {
384 clearOrClose();
385 mUrlInput.setText("");
386 }
387 }
388
389 private static boolean urlHasSitePrefs(String url) {
390 if (TextUtils.isEmpty(url)) {
391 return false;
392 }
393
394 for (int i = 0; i < noSitePrefs.length; i++) {
395 if (url.startsWith(noSitePrefs[i])) {
396 return false;
397 }
398 }
399 return true;
400 }
401
402 private void stopOrRefresh() {
403 if (mUiController == null) return;
404 if (mTitleBar.isInLoad()) {
405 mUiController.stopLoading();
406 } else {
407 if (mUiController.getCurrentTopWebView() != null) {
408 stopEditingUrl();
409 mUiController.getCurrentTopWebView().reload();
410 }
411 }
412 }
413
414 private void clearOrClose() {
415 if (TextUtils.isEmpty(mUrlInput.getText())) {
416 // close
417 mUrlInput.clearFocus();
418 } else {
419 // clear
420 mUrlInput.setText("");
Enrico Ros1f5a0952014-11-18 20:15:48 -0800421 }
422 }
423
424 void showMenu(View anchor) {
425 Activity activity = mUiController.getActivity();
426 if (mPopupMenu == null) {
427 mPopupMenu = new PopupMenu(getContext(), anchor);
428 mPopupMenu.setOnMenuItemClickListener(this);
429 mPopupMenu.setOnDismissListener(this);
430 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
431 mPopupMenu = null;
432 return;
433 }
434 }
435 Menu menu = mPopupMenu.getMenu();
436
437 if (mUiController instanceof Controller) {
438 Controller controller = (Controller) mUiController;
439 if (controller.onPrepareOptionsMenu(menu)) {
440 mOverflowMenuShowing = true;
441 }
442 }
John Reck0f602f32011-07-07 15:38:43 -0700443 }
444
445 @Override
446 public void onFocusChange(View view, boolean hasFocus) {
447 // if losing focus and not in touch mode, leave as is
448 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
449 setFocusState(hasFocus);
450 }
451 if (hasFocus) {
Kevin Hart055e6d82014-12-19 15:53:19 -0800452 Engine.warmUpChildProcessAsync(mUiController.getActivity().getApplicationContext());
John Reck0f602f32011-07-07 15:38:43 -0700453 mBaseUi.showTitleBar();
Dany Rybnikovbcd37da2015-04-20 11:43:15 +0300454 if (!BrowserSettings.getInstance().isPowerSaveModeEnabled()) {
455 //Notify about anticipated network activity
456 NetworkServices.hintUpcomingUserActivity();
457 }
John Reck0f602f32011-07-07 15:38:43 -0700458 } else if (!mUrlInput.needsUpdate()) {
459 mUrlInput.dismissDropDown();
460 mUrlInput.hideIME();
461 if (mUrlInput.getText().length() == 0) {
462 Tab currentTab = mUiController.getTabControl().getCurrentTab();
463 if (currentTab != null) {
John Reck434e9f82011-08-10 18:16:52 -0700464 setDisplayTitle(currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700465 }
466 }
467 mBaseUi.suggestHideTitleBar();
468 }
469 mUrlInput.clearNeedsUpdate();
470 }
471
472 protected void setFocusState(boolean focus) {
473 }
474
John Reck0f602f32011-07-07 15:38:43 -0700475 public boolean isEditingUrl() {
476 return mUrlInput.hasFocus();
477 }
478
479 void stopEditingUrl() {
Michael Kolb0b129122012-06-04 16:31:58 -0700480 WebView currentTopWebView = mUiController.getCurrentTopWebView();
481 if (currentTopWebView != null) {
482 currentTopWebView.requestFocus();
483 }
John Reck0f602f32011-07-07 15:38:43 -0700484 }
485
486 void setDisplayTitle(String title) {
487 if (!isEditingUrl()) {
Michael Kolb29aab402012-05-29 17:22:35 -0700488 if (!title.equals(mUrlInput.getText().toString())) {
489 mUrlInput.setText(title, false);
490 }
John Reck0f602f32011-07-07 15:38:43 -0700491 }
492 }
493
John Reck0f602f32011-07-07 15:38:43 -0700494 void setIncognitoMode(boolean incognito) {
495 mUrlInput.setIncognitoMode(incognito);
496 }
497
498 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000499 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700500 }
501
502 // UrlInputListener implementation
503
504 /**
505 * callback from suggestion dropdown
506 * user selected a suggestion
507 */
508 @Override
509 public void onAction(String text, String extra, String source) {
Michael Kolb0b129122012-06-04 16:31:58 -0700510 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700511 if (UrlInputView.TYPED.equals(source)) {
kaiyizc4ada322013-07-30 09:58:07 +0800512 String url = null;
Panos Thomas4bdb5252014-11-13 16:20:11 -0800513 boolean wap2estore = BrowserConfig.getInstance(getContext())
514 .hasFeature(BrowserConfig.Feature.WAP2ESTORE);
Vivek Sekhar5e631472014-03-31 23:59:10 -0700515 if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text)
516 || isMakeCallTypeUrl(text)) {
kaiyizc4ada322013-07-30 09:58:07 +0800517 url = text;
518 } else {
519 url = UrlUtils.smartUrlFilter(text, false);
520 }
521
John Reck0f602f32011-07-07 15:38:43 -0700522 Tab t = mBaseUi.getActiveTab();
523 // Only shortcut javascript URIs for now, as there is special
524 // logic in UrlHandler for other schemas
525 if (url != null && t != null && url.startsWith("javascript:")) {
526 mUiController.loadUrl(t, url);
527 setDisplayTitle(text);
528 return;
529 }
kaiyizc4ada322013-07-30 09:58:07 +0800530
531 // add for carrier wap2estore feature
532 if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700533 if (handleEstoreTypeUrl(url)) {
534 setDisplayTitle(text);
535 return;
536 }
kaiyizc4ada322013-07-30 09:58:07 +0800537 }
kaiyiz950eca22013-08-08 11:01:28 +0800538 // add for rtsp scheme feature
539 if (url != null && t != null && isRtspTypeUrl(url)) {
540 if (handleRtspTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700541 setDisplayTitle(text);
kaiyiz950eca22013-08-08 11:01:28 +0800542 return;
543 }
544 }
Vivek Sekhar5e631472014-03-31 23:59:10 -0700545 // add for "wtai://wp/mc;" scheme feature
546 if (url != null && t != null && isMakeCallTypeUrl(url)) {
547 if (handleMakeCallTypeUrl(url)) {
548 return;
549 }
550 }
John Reck0f602f32011-07-07 15:38:43 -0700551 }
552 Intent i = new Intent();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700553 String action = Intent.ACTION_SEARCH;
John Reck0f602f32011-07-07 15:38:43 -0700554 i.setAction(action);
555 i.putExtra(SearchManager.QUERY, text);
556 if (extra != null) {
557 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
558 }
559 if (source != null) {
560 Bundle appData = new Bundle();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800561 appData.putString("source", source);
562 i.putExtra("source", appData);
John Reck0f602f32011-07-07 15:38:43 -0700563 }
564 mUiController.handleNewIntent(i);
565 setDisplayTitle(text);
566 }
567
Vivek Sekhar5e631472014-03-31 23:59:10 -0700568 private boolean isMakeCallTypeUrl(String url) {
569 String utf8Url = null;
570 try {
571 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
572 } catch (UnsupportedEncodingException e) {
573 Log.e(TAG, "err " + e);
574 }
575 if (utf8Url != null && utf8Url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
576 return true;
577 }
578 return false;
579 }
580
581 private boolean handleMakeCallTypeUrl(String url) {
582 // wtai://wp/mc;number
583 // number=string(phone-number)
584 if (url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
585 Intent intent = new Intent(Intent.ACTION_VIEW,
586 Uri.parse(WebView.SCHEME_TEL +
587 url.substring(UrlHandler.SCHEME_WTAI_MC.length())));
588 getContext().startActivity(intent);
589 // before leaving BrowserActivity, close the empty child tab.
590 // If a new tab is created through JavaScript open to load this
591 // url, we would like to close it as we will load this url in a
592 // different Activity.
593 Tab current = mUiController.getCurrentTab();
594 if (current != null
595 && current.getWebView().copyBackForwardList().getSize() == 0) {
596 mUiController.closeCurrentTab();
597 }
598 return true;
599 }
600 return false;
601 }
602
kaiyizc4ada322013-07-30 09:58:07 +0800603 private boolean isEstoreTypeUrl(String url) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700604 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800605 return true;
606 }
607 return false;
608 }
609
Vivek Sekharb54614f2014-05-01 19:03:37 -0700610 private boolean handleEstoreTypeUrl(String url) {
611 if (url.getBytes().length > 256) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800612 Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700613 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800614 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700615
616 Intent intent;
617 // perform generic parsing of the URI to turn it into an Intent.
618 try {
619 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
620 } catch (URISyntaxException ex) {
621 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
622 return false;
623 }
624
kaiyizc4ada322013-07-30 09:58:07 +0800625 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800626 getContext().startActivity(intent);
kaiyizc4ada322013-07-30 09:58:07 +0800627 } catch (ActivityNotFoundException ex) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800628 String downloadUrl = getContext().getResources().getString(R.string.estore_homepage);
kaiyizc4ada322013-07-30 09:58:07 +0800629 mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800630 Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800631 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700632
633 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800634 }
635
kaiyiz950eca22013-08-08 11:01:28 +0800636 private boolean isRtspTypeUrl(String url) {
637 String utf8Url = null;
638 try {
639 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
640 } catch (UnsupportedEncodingException e) {
641 Log.e(TAG, "err " + e);
642 }
643 if (utf8Url != null && utf8Url.startsWith("rtsp://")) {
644 return true;
645 }
646 return false;
647 }
648
649 private boolean handleRtspTypeUrl(String url) {
650 Intent intent;
651 // perform generic parsing of the URI to turn it into an Intent.
652 try {
653 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
654 } catch (URISyntaxException ex) {
655 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
656 return false;
657 }
658
659 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800660 getContext().startActivity(intent);
kaiyiz950eca22013-08-08 11:01:28 +0800661 } catch (ActivityNotFoundException ex) {
662 Log.w("Browser", "No resolveActivity " + url);
663 return false;
664 }
665 return true;
666 }
667
John Reck0f602f32011-07-07 15:38:43 -0700668 @Override
669 public void onDismiss() {
670 final Tab currentTab = mBaseUi.getActiveTab();
671 mBaseUi.hideTitleBar();
672 post(new Runnable() {
673 public void run() {
674 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700675 if (currentTab != null) {
John Reck0f602f32011-07-07 15:38:43 -0700676 setDisplayTitle(currentTab.getUrl());
677 }
678 }
679 });
680 }
681
682 /**
683 * callback from the suggestion dropdown
684 * copy text to input field and stay in edit mode
685 */
686 @Override
687 public void onCopySuggestion(String text) {
688 mUrlInput.setText(text, true);
689 if (text != null) {
690 mUrlInput.setSelection(text.length());
691 }
692 }
693
694 public void setCurrentUrlIsBookmark(boolean isBookmark) {
695 }
696
697 @Override
698 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
699 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
700 // catch back key in order to do slightly more cleanup than usual
Michael Kolb0b129122012-06-04 16:31:58 -0700701 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700702 }
703 return super.dispatchKeyEventPreIme(evt);
704 }
705
John Reck0f602f32011-07-07 15:38:43 -0700706 /**
707 * called from the Ui when the user wants to edit
708 * @param clearInput clear the input field
709 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700710 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700711 // editing takes preference of progress
712 setVisibility(View.VISIBLE);
John Reck0f602f32011-07-07 15:38:43 -0700713 if (!mUrlInput.hasFocus()) {
714 mUrlInput.requestFocus();
715 }
716 if (clearInput) {
717 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700718 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700719 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700720 mUrlInput.showIME();
721 }
John Reck0f602f32011-07-07 15:38:43 -0700722 }
723
724 public void onProgressStarted() {
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700725 mFaviconTile.setBadgeBlockedObjectsCount(0);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700726 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNKNOWN);
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700727 mFaviconTile.setBadgeHasCertIssues(false);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700728 mFaviconTile.replaceFavicon(mDefaultFavicon);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700729 mSecurityState = Tab.SecurityState.SECURITY_STATE_NOT_SECURE;
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700730 mHandler.removeMessages(WEBREFINER_COUNTER_MSG);
731 mHandler.sendEmptyMessageDelayed(WEBREFINER_COUNTER_MSG,
732 WEBREFINER_COUNTER_MSG_DELAY);
733 mStopButton.setImageResource(R.drawable.ic_action_stop);
734 mStopButton.setContentDescription(getResources().
735 getString(R.string.accessibility_button_stop));
John Reck0f602f32011-07-07 15:38:43 -0700736 }
737
738 public void onProgressStopped() {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700739 if (!isEditingUrl()) {
740 mFaviconTile.setVisibility(View.VISIBLE);
741 }
742 mStopButton.setImageResource(R.drawable.ic_action_reload);
743 mStopButton.setContentDescription(getResources().
744 getString(R.string.accessibility_button_refresh));
John Reck0f602f32011-07-07 15:38:43 -0700745 }
746
John Reck419f6b42011-08-16 16:10:51 -0700747 public void onTabDataChanged(Tab tab) {
748 }
749
Michael Kolb0b129122012-06-04 16:31:58 -0700750 public void onVoiceResult(String s) {
751 startEditingUrl(true, true);
752 onCopySuggestion(s);
753 }
754
Narayan Kamathf3174a52011-11-17 14:43:32 +0000755 @Override
756 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
757
758 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700759 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000760
761 @Override
762 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700763
Enrico Ros1f5a0952014-11-18 20:15:48 -0800764 @Override
765 public void onStateChanged(int state) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700766 mVoiceButton.setVisibility(View.GONE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800767 switch(state) {
768 case STATE_NORMAL:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700769 mFaviconTile.setVisibility(View.VISIBLE);
770 mMagnify.setVisibility(View.GONE);
771 mClearButton.setVisibility(View.GONE);
Sagar Dhawanaff56c92015-07-13 15:57:11 -0700772 mMore.setVisibility(View.VISIBLE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700773 if (mUiController != null) {
774 Tab currentTab = mUiController.getCurrentTab();
775 if (currentTab != null){
776 if (TextUtils.isEmpty(currentTab.getUrl())) {
777 mFaviconTile.setVisibility(View.GONE);
778 mMagnify.setVisibility(View.VISIBLE);
779 }
780 }
781 mUiController.setWindowDimming(0.0f);
782 }
783
Enrico Ros1f5a0952014-11-18 20:15:48 -0800784 break;
785 case STATE_HIGHLIGHTED:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700786 mFaviconTile.setVisibility(View.GONE);
787 mMagnify.setVisibility(View.GONE);
788 mClearButton.setVisibility(View.GONE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800789 mMore.setVisibility(View.GONE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700790 if (mUiController != null) {
791 mUiController.setWindowDimming(0.75f);
792 if (mUiController.supportsVoice()) {
793 mVoiceButton.setVisibility(View.VISIBLE);
794 }
795 }
Enrico Ros1f5a0952014-11-18 20:15:48 -0800796 break;
797 case STATE_EDITED:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700798 mFaviconTile.setVisibility(View.GONE);
799 mMagnify.setVisibility(View.VISIBLE);
800 mClearButton.setVisibility(View.VISIBLE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800801 mMore.setVisibility(View.GONE);
802 break;
803 }
804 }
805
806 public boolean isMenuShowing() {
807 return mOverflowMenuShowing;
808 }
809
810
811 @Override
812 public void onDismiss(PopupMenu popupMenu) {
813 if (popupMenu == mPopupMenu) {
814 onMenuHidden();
815 }
816 }
817
818 private void onMenuHidden() {
819 mOverflowMenuShowing = false;
820 mBaseUi.showTitleBarForDuration();
821 }
822
823
824 @Override
825 public boolean onMenuItemClick(MenuItem item) {
826 return mUiController.onOptionsItemSelected(item);
827 }
John Reck0f602f32011-07-07 15:38:43 -0700828}