blob: d1054b9c108cb51a5b68d100d763bc429eccc948 [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;
Pankaj Garg32e1b942015-06-03 18:13:24 -070031import android.os.Build;
John Reck0f602f32011-07-07 15:38:43 -070032import android.os.Bundle;
Pankaj Garg66f8cef2015-07-07 17:29:03 -070033import android.os.Handler;
34import android.os.Message;
Narayan Kamathf3174a52011-11-17 14:43:32 +000035import android.text.Editable;
Pankaj Garg66f8cef2015-07-07 17:29:03 -070036import android.text.TextUtils;
Narayan Kamathf3174a52011-11-17 14:43:32 +000037import android.text.TextWatcher;
John Reck0f602f32011-07-07 15:38:43 -070038import android.util.AttributeSet;
kaiyizc4ada322013-07-30 09:58:07 +080039import android.util.Log;
John Reck0f602f32011-07-07 15:38:43 -070040import android.view.KeyEvent;
Enrico Ros1f5a0952014-11-18 20:15:48 -080041import android.view.Menu;
42import android.view.MenuItem;
John Reck0f602f32011-07-07 15:38:43 -070043import android.view.View;
44import android.view.View.OnClickListener;
45import android.view.View.OnFocusChangeListener;
John Reck0f602f32011-07-07 15:38:43 -070046import android.widget.ImageView;
47import android.widget.LinearLayout;
Enrico Ros1f5a0952014-11-18 20:15:48 -080048import android.widget.PopupMenu;
kaiyizc4ada322013-07-30 09:58:07 +080049import android.widget.Toast;
John Reck0f602f32011-07-07 15:38:43 -070050
Bijan Amirzada41242f22014-03-21 12:12:18 -070051import com.android.browser.UrlInputView.UrlInputListener;
Pankaj Garg32e1b942015-06-03 18:13:24 -070052import com.android.browser.preferences.SiteSpecificPreferencesFragment;
John Reck0f602f32011-07-07 15:38:43 -070053
Pankaj Garg32e1b942015-06-03 18:13:24 -070054import java.io.ByteArrayOutputStream;
kaiyizc4ada322013-07-30 09:58:07 +080055import java.io.UnsupportedEncodingException;
Pankaj Garg32e1b942015-06-03 18:13:24 -070056import java.net.MalformedURLException;
kaiyiz950eca22013-08-08 11:01:28 +080057import java.net.URISyntaxException;
Pankaj Garg32e1b942015-06-03 18:13:24 -070058import java.net.URL;
kaiyizc4ada322013-07-30 09:58:07 +080059
Dany Rybnikovbcd37da2015-04-20 11:43:15 +030060import org.codeaurora.net.NetworkServices;
Pankaj Garg32e1b942015-06-03 18:13:24 -070061import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080062import org.codeaurora.swe.WebView;
Pankaj Garg32e1b942015-06-03 18:13:24 -070063import org.codeaurora.swe.util.ColorUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080064
John Reck42229bc2011-08-19 13:26:43 -070065public class NavigationBarBase extends LinearLayout implements
66 OnClickListener, UrlInputListener, OnFocusChangeListener,
Enrico Ros1f5a0952014-11-18 20:15:48 -080067 TextWatcher, UrlInputView.StateListener,
68 PopupMenu.OnMenuItemClickListener, PopupMenu.OnDismissListener {
John Reck0f602f32011-07-07 15:38:43 -070069
kaiyizc4ada322013-07-30 09:58:07 +080070 private final static String TAG = "NavigationBarBase";
71
John Reck0f602f32011-07-07 15:38:43 -070072 protected BaseUi mBaseUi;
73 protected TitleBar mTitleBar;
74 protected UiController mUiController;
75 protected UrlInputView mUrlInput;
Pankaj Garg66f8cef2015-07-07 17:29:03 -070076 protected ImageView mStopButton;
John Reck0f602f32011-07-07 15:38:43 -070077
Pankaj Garg66f8cef2015-07-07 17:29:03 -070078 private SiteTileView mFaviconTile;
79 private ImageView mMagnify;
80 private View mVoiceButton;
81 private ImageView mClearButton;
Enrico Ros1f5a0952014-11-18 20:15:48 -080082 private View mMore;
83 private PopupMenu mPopupMenu;
84 private boolean mOverflowMenuShowing;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070085
Pankaj Garg66f8cef2015-07-07 17:29:03 -070086 private static Bitmap mDefaultFavicon;
87
Pankaj Garg32e1b942015-06-03 18:13:24 -070088 private int mStatusBarColor;
89 private static int mDefaultStatusBarColor = -1;
90
Pankaj Garg66f8cef2015-07-07 17:29:03 -070091 private static final int WEBREFINER_COUNTER_MSG = 4242;
92 private static final int WEBREFINER_COUNTER_MSG_DELAY = 3000;
93 private Handler mHandler;
94
Pankaj Garg8d2e98f2015-08-07 10:01:49 -070095 protected int mTrustLevel = SiteTileView.TRUST_UNKNOWN;
96
Pankaj Garg66f8cef2015-07-07 17:29:03 -070097 private static final String noSitePrefs[] = {
Axesh R. Ajmera6fc73692015-08-11 16:32:10 -070098 "chrome://",
Pankaj Garg66f8cef2015-07-07 17:29:03 -070099 "about:",
100 "content:",
101 };
102
John Reck0f602f32011-07-07 15:38:43 -0700103 public NavigationBarBase(Context context) {
104 super(context);
105 }
106
107 public NavigationBarBase(Context context, AttributeSet attrs) {
108 super(context, attrs);
109 }
110
111 public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) {
112 super(context, attrs, defStyle);
113 }
114
115 @Override
116 protected void onFinishInflate() {
117 super.onFinishInflate();
John Reck0f602f32011-07-07 15:38:43 -0700118 mUrlInput = (UrlInputView) findViewById(R.id.url);
119 mUrlInput.setUrlInputListener(this);
John Reck0f602f32011-07-07 15:38:43 -0700120 mUrlInput.setOnFocusChangeListener(this);
121 mUrlInput.setSelectAllOnFocus(true);
Narayan Kamathf3174a52011-11-17 14:43:32 +0000122 mUrlInput.addTextChangedListener(this);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800123 mMore = findViewById(R.id.more_browser_settings);
124 mMore.setOnClickListener(this);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700125 mMagnify = (ImageView) findViewById(R.id.magnify);
126 mMagnify.setOnClickListener(this);
127 mFaviconTile = (SiteTileView) findViewById(R.id.favicon_view);
128 mFaviconTile.setOnClickListener(this);
129 mVoiceButton = findViewById(R.id.voice);
130 mVoiceButton.setOnClickListener(this);
131 mClearButton = (ImageView) findViewById(R.id.clear);
132 mClearButton.setOnClickListener(this);
133 mStopButton = (ImageView) findViewById(R.id.stop);
134 mStopButton.setOnClickListener(this);
135
136 mDefaultFavicon = BitmapFactory.decodeResource(getResources(),
137 R.drawable.ic_deco_favicon_normal);
138
139 mHandler = new Handler() {
140 @Override
141 public void handleMessage(Message m) {
142 switch (m.what) {
143 case WEBREFINER_COUNTER_MSG:
144 WebView wv = mUiController.getCurrentTopWebView();
145 if (wv != null && WebRefiner.isInitialized()) {
146 int count = WebRefiner.getInstance().getBlockedURLCount(wv);
147 if (count > 0) {
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700148 mFaviconTile.setBadgeBlockedObjectsCount(count);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700149 }
150 }
151 mHandler.sendEmptyMessageDelayed(WEBREFINER_COUNTER_MSG,
152 WEBREFINER_COUNTER_MSG_DELAY);
153 break;
154 }
155 }
156 };
John Reck0f602f32011-07-07 15:38:43 -0700157 }
158
159 public void setTitleBar(TitleBar titleBar) {
160 mTitleBar = titleBar;
161 mBaseUi = mTitleBar.getUi();
162 mUiController = mTitleBar.getUiController();
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700163 mUrlInput.setController(mUiController);
John Reck0f602f32011-07-07 15:38:43 -0700164 }
165
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700166 public void setSecurityState(Tab.SecurityState securityState) {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700167 switch (securityState) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700168 case SECURITY_STATE_SECURE:
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700169 mTrustLevel = SiteTileView.TRUST_TRUSTED;
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700170 mFaviconTile.setBadgeHasCertIssues(false);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700171 break;
172 case SECURITY_STATE_MIXED:
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700173 mTrustLevel = SiteTileView.TRUST_UNTRUSTED;
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700174 mFaviconTile.setBadgeHasCertIssues(true);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700175 mTitleBar.showTopControls(false);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700176 break;
177 case SECURITY_STATE_BAD_CERTIFICATE:
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700178 mTrustLevel = SiteTileView.TRUST_AVOID;
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700179 mFaviconTile.setBadgeHasCertIssues(true);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700180 mTitleBar.showTopControls(false);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700181 break;
182 case SECURITY_STATE_NOT_SECURE:
183 default:
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700184 mTrustLevel = SiteTileView.TRUST_UNKNOWN;
John Reck0f602f32011-07-07 15:38:43 -0700185 }
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700186 mFaviconTile.setTrustLevel(mTrustLevel);
187 }
188
189 public int getTrustLevel() {
190 return mTrustLevel;
John Reck0f602f32011-07-07 15:38:43 -0700191 }
192
Pankaj Garg32e1b942015-06-03 18:13:24 -0700193 public static int adjustColor(int color, float hueMultiplier,
194 float saturationMultiplier, float valueMultiplier) {
195 float[] hsv = new float[3];
196 Color.colorToHSV(color, hsv);
197 hsv[0] *= hueMultiplier;
198 hsv[1] *= saturationMultiplier;
199 hsv[2] *= valueMultiplier;
200 return Color.HSVToColor(Color.alpha(color), hsv);
201 }
202
203 public static void setStatusAndNavigationBarColor(final Activity activity, int color) {
204 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
205 int currentColor = activity.getWindow().getStatusBarColor();
206 Integer from = currentColor;
207 Integer to = color;
208 ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
209
210 if (mDefaultStatusBarColor == -1) {
211 mDefaultStatusBarColor = activity.getWindow().getStatusBarColor();
212 }
213
214 animator.addUpdateListener(
215 new ValueAnimator.AnimatorUpdateListener() {
216 @Override
217 public void onAnimationUpdate(ValueAnimator animation) {
218 Integer value = (Integer) animation.getAnimatedValue();
219 activity.getWindow().setStatusBarColor(value.intValue());
220 //activity.getWindow().setNavigationBarColor(value.intValue());
221 }
222 }
223 );
224 animator.start();
225 }
226 }
227
228 private void updateSiteIconColor(String urlString, int color) {
229 try {
230 URL url = new URL(urlString);
231 String host = url.getHost();
232 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
233 int currentColor = prefs.getInt(host + ":color", 0);
234 if (currentColor != color) {
235 SharedPreferences.Editor editor = prefs.edit();
236 editor.remove(host + ":color");
237 editor.putInt(host + ":color", color);
238 editor.commit();
239 }
240 } catch (MalformedURLException e) {
241 }
242 }
243
244 public static int getSiteIconColor(String urlString) {
245 try {
246 URL url = new URL(urlString);
247 String host = url.getHost();
248 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
249 return prefs.getInt(host + ":color", 0);
250 } catch (MalformedURLException e) {
251 return 0;
252 }
253 }
254
255 public static int getDefaultStatusBarColor() {
256 return mDefaultStatusBarColor;
257 }
258
John Reck0f602f32011-07-07 15:38:43 -0700259 public void setFavicon(Bitmap icon) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700260 int color = ColorUtils.getDominantColorForBitmap(icon);
261 Tab tab = mUiController.getCurrentTab();
262
263 if (tab != null) {
264 if (tab.hasFavicon()) {
265 updateSiteIconColor(tab.getUrl(), color);
266 setStatusAndNavigationBarColor(mUiController.getActivity(),
267 adjustColor(color, 1, 1, 0.7f));
268 } else {
269 color = getSiteIconColor(tab.getUrl());
270 if (color != 0) {
271 setStatusAndNavigationBarColor(mUiController.getActivity(),
272 adjustColor(color, 1, 1, 0.7f));
273 } else {
274 setStatusAndNavigationBarColor(mUiController.getActivity(),
275 mDefaultStatusBarColor);
276 }
277 }
278 } else {
279 setStatusAndNavigationBarColor(mUiController.getActivity(), mDefaultStatusBarColor);
280 }
281
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700282 //Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
283
284 if (mFaviconTile != null) {
285 mFaviconTile.replaceFavicon(mUiController.getCurrentTopWebView().getFavicon());
286 }
John Reck0f602f32011-07-07 15:38:43 -0700287 }
288
Pankaj Garg32e1b942015-06-03 18:13:24 -0700289 protected void showSiteSpecificSettings() {
290 WebView wv = mUiController.getCurrentTopWebView();
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700291 int ads = 0;
292 int tracker = 0;
293 int malware = 0;
Pankaj Garg32e1b942015-06-03 18:13:24 -0700294
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700295 WebRefiner webRefiner = WebRefiner.getInstance();
296 if (wv != null && webRefiner != null) {
297 WebRefiner.PageInfo pageInfo = webRefiner.getPageInfo(wv);
298 if (pageInfo != null) {
299 for (WebRefiner.MatchedURLInfo urlInfo : pageInfo.mMatchedURLInfoList) {
Pankaj Gargee992032015-07-24 15:59:36 -0700300 if (urlInfo.mActionTaken == WebRefiner.MatchedURLInfo.ACTION_BLOCKED) {
301 switch (urlInfo.mMatchedFilterCategory) {
302 case WebRefiner.RuleSet.CATEGORY_ADS:
303 ads++;
304 break;
305 case WebRefiner.RuleSet.CATEGORY_TRACKERS:
306 tracker++;
307 break;
308 case WebRefiner.RuleSet.CATEGORY_MALWARE_DOMAINS:
309 malware++;
310 break;
311 }
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700312 }
313 }
314 }
Pankaj Garg32e1b942015-06-03 18:13:24 -0700315 }
316
317 Bundle bundle = new Bundle();
318 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE,
319 mUiController.getCurrentTab().getUrl());
Sagar Dhawane28023a2015-08-13 16:40:25 -0700320 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE_TITLE,
321 mUiController.getCurrentTab().getTitle());
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700322 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_ADS_INFO, ads);
323 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_TRACKER_INFO, tracker);
324 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_MALWARE_INFO, malware);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700325
326 bundle.putParcelable(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT,
327 SslCertificate.saveState(wv.getCertificate()));
328
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700329 Tab.SecurityState securityState = Tab.getWebViewSecurityState(
330 mUiController.getCurrentTab().getWebView());
331 if (securityState == Tab.SecurityState.SECURITY_STATE_MIXED) {
332 bundle.putBoolean(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_MIXED, true);
333 } else if (securityState == Tab.SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
334 bundle.putBoolean(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_BAD, true);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700335 }
336
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700337 Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
Pankaj Garg32e1b942015-06-03 18:13:24 -0700338 if (favicon != null) {
339 ByteArrayOutputStream baos = new ByteArrayOutputStream();
340 favicon.compress(Bitmap.CompressFormat.PNG, 50, baos);
341 bundle.putByteArray(SiteSpecificPreferencesFragment.EXTRA_FAVICON,
342 baos.toByteArray());
343 }
344 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(
345 mUiController.getActivity(),
346 SiteSpecificPreferencesFragment.class.getName(), bundle, 0);
347 }
348
John Reck0f602f32011-07-07 15:38:43 -0700349 @Override
350 public void onClick(View v) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700351 Tab currentTab = mUiController.getCurrentTab();
Vivek Sekharae3b1792015-08-03 12:26:41 -0700352 WebView wv = currentTab.getWebView();
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700353 String url = null;
354 if (currentTab != null){
355 url = currentTab.getUrl();
356 }
Enrico Ros1f5a0952014-11-18 20:15:48 -0800357 if (mMore == v) {
358 showMenu(mMore);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700359 } else if (mFaviconTile == v) {
Vivek Sekharae3b1792015-08-03 12:26:41 -0700360 if (urlHasSitePrefs(url) && (wv != null && !wv.isShowingInterstitialPage()) ){
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700361 showSiteSpecificSettings();
362 }
363 } else if (mMagnify == v) {
364 startEditingUrl(true, true);
365 } else if (mVoiceButton == v) {
366 mUiController.startVoiceRecognizer();
367 } else if (mStopButton == v) {
368 stopOrRefresh();
369 } else if (mClearButton == v) {
370 clearOrClose();
371 mUrlInput.setText("");
372 }
373 }
374
375 private static boolean urlHasSitePrefs(String url) {
376 if (TextUtils.isEmpty(url)) {
377 return false;
378 }
379
380 for (int i = 0; i < noSitePrefs.length; i++) {
381 if (url.startsWith(noSitePrefs[i])) {
382 return false;
383 }
384 }
385 return true;
386 }
387
388 private void stopOrRefresh() {
389 if (mUiController == null) return;
390 if (mTitleBar.isInLoad()) {
391 mUiController.stopLoading();
392 } else {
393 if (mUiController.getCurrentTopWebView() != null) {
394 stopEditingUrl();
395 mUiController.getCurrentTopWebView().reload();
396 }
397 }
398 }
399
400 private void clearOrClose() {
401 if (TextUtils.isEmpty(mUrlInput.getText())) {
402 // close
403 mUrlInput.clearFocus();
404 } else {
405 // clear
406 mUrlInput.setText("");
Enrico Ros1f5a0952014-11-18 20:15:48 -0800407 }
408 }
409
410 void showMenu(View anchor) {
411 Activity activity = mUiController.getActivity();
412 if (mPopupMenu == null) {
413 mPopupMenu = new PopupMenu(getContext(), anchor);
414 mPopupMenu.setOnMenuItemClickListener(this);
415 mPopupMenu.setOnDismissListener(this);
416 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
417 mPopupMenu = null;
418 return;
419 }
420 }
421 Menu menu = mPopupMenu.getMenu();
422
423 if (mUiController instanceof Controller) {
424 Controller controller = (Controller) mUiController;
425 if (controller.onPrepareOptionsMenu(menu)) {
426 mOverflowMenuShowing = true;
427 }
428 }
John Reck0f602f32011-07-07 15:38:43 -0700429 }
430
431 @Override
432 public void onFocusChange(View view, boolean hasFocus) {
433 // if losing focus and not in touch mode, leave as is
434 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
435 setFocusState(hasFocus);
436 }
437 if (hasFocus) {
438 mBaseUi.showTitleBar();
Dany Rybnikovbcd37da2015-04-20 11:43:15 +0300439 if (!BrowserSettings.getInstance().isPowerSaveModeEnabled()) {
440 //Notify about anticipated network activity
441 NetworkServices.hintUpcomingUserActivity();
442 }
John Reck0f602f32011-07-07 15:38:43 -0700443 } else if (!mUrlInput.needsUpdate()) {
444 mUrlInput.dismissDropDown();
445 mUrlInput.hideIME();
446 if (mUrlInput.getText().length() == 0) {
447 Tab currentTab = mUiController.getTabControl().getCurrentTab();
448 if (currentTab != null) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700449 setDisplayTitle(currentTab.getTitle(), currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700450 }
451 }
John Reck0f602f32011-07-07 15:38:43 -0700452 }
453 mUrlInput.clearNeedsUpdate();
454 }
455
456 protected void setFocusState(boolean focus) {
457 }
458
John Reck0f602f32011-07-07 15:38:43 -0700459 public boolean isEditingUrl() {
460 return mUrlInput.hasFocus();
461 }
462
463 void stopEditingUrl() {
Michael Kolb0b129122012-06-04 16:31:58 -0700464 WebView currentTopWebView = mUiController.getCurrentTopWebView();
465 if (currentTopWebView != null) {
466 currentTopWebView.requestFocus();
467 }
John Reck0f602f32011-07-07 15:38:43 -0700468 }
469
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700470 void setDisplayTitle(String title, String url) {
John Reck0f602f32011-07-07 15:38:43 -0700471 if (!isEditingUrl()) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700472 if (!TextUtils.isEmpty(title)) {
473 if (mTrustLevel == SiteTileView.TRUST_TRUSTED) {
474 if (!title.equals(mUrlInput.getText().toString())) {
475 mUrlInput.setText(title, false);
476 }
477 return;
478 }
479 }
480 if (!url.equals(mUrlInput.getText().toString())) {
481 mUrlInput.setText(url, false);
Michael Kolb29aab402012-05-29 17:22:35 -0700482 }
John Reck0f602f32011-07-07 15:38:43 -0700483 }
484 }
485
John Reck0f602f32011-07-07 15:38:43 -0700486 void setIncognitoMode(boolean incognito) {
487 mUrlInput.setIncognitoMode(incognito);
488 }
489
490 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000491 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700492 }
493
494 // UrlInputListener implementation
495
496 /**
497 * callback from suggestion dropdown
498 * user selected a suggestion
499 */
500 @Override
501 public void onAction(String text, String extra, String source) {
Michael Kolb0b129122012-06-04 16:31:58 -0700502 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700503 if (UrlInputView.TYPED.equals(source)) {
kaiyizc4ada322013-07-30 09:58:07 +0800504 String url = null;
Panos Thomas4bdb5252014-11-13 16:20:11 -0800505 boolean wap2estore = BrowserConfig.getInstance(getContext())
506 .hasFeature(BrowserConfig.Feature.WAP2ESTORE);
Vivek Sekhar5e631472014-03-31 23:59:10 -0700507 if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text)
508 || isMakeCallTypeUrl(text)) {
kaiyizc4ada322013-07-30 09:58:07 +0800509 url = text;
510 } else {
511 url = UrlUtils.smartUrlFilter(text, false);
512 }
513
John Reck0f602f32011-07-07 15:38:43 -0700514 Tab t = mBaseUi.getActiveTab();
515 // Only shortcut javascript URIs for now, as there is special
516 // logic in UrlHandler for other schemas
Axesh R. Ajmera6fc73692015-08-11 16:32:10 -0700517 if (url != null && t != null && url.startsWith("javascript:")) {
John Reck0f602f32011-07-07 15:38:43 -0700518 mUiController.loadUrl(t, url);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700519 setDisplayTitle(null, text);
John Reck0f602f32011-07-07 15:38:43 -0700520 return;
521 }
kaiyizc4ada322013-07-30 09:58:07 +0800522
523 // add for carrier wap2estore feature
524 if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700525 if (handleEstoreTypeUrl(url)) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700526 setDisplayTitle(null, text);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700527 return;
528 }
kaiyizc4ada322013-07-30 09:58:07 +0800529 }
kaiyiz950eca22013-08-08 11:01:28 +0800530 // add for rtsp scheme feature
531 if (url != null && t != null && isRtspTypeUrl(url)) {
532 if (handleRtspTypeUrl(url)) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700533 setDisplayTitle(null, text);
kaiyiz950eca22013-08-08 11:01:28 +0800534 return;
535 }
536 }
Vivek Sekhar5e631472014-03-31 23:59:10 -0700537 // add for "wtai://wp/mc;" scheme feature
538 if (url != null && t != null && isMakeCallTypeUrl(url)) {
539 if (handleMakeCallTypeUrl(url)) {
540 return;
541 }
542 }
John Reck0f602f32011-07-07 15:38:43 -0700543 }
544 Intent i = new Intent();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700545 String action = Intent.ACTION_SEARCH;
John Reck0f602f32011-07-07 15:38:43 -0700546 i.setAction(action);
547 i.putExtra(SearchManager.QUERY, text);
548 if (extra != null) {
549 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
550 }
551 if (source != null) {
552 Bundle appData = new Bundle();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800553 appData.putString("source", source);
554 i.putExtra("source", appData);
John Reck0f602f32011-07-07 15:38:43 -0700555 }
556 mUiController.handleNewIntent(i);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700557 setDisplayTitle(null, text);
John Reck0f602f32011-07-07 15:38:43 -0700558 }
559
Vivek Sekhar5e631472014-03-31 23:59:10 -0700560 private boolean isMakeCallTypeUrl(String url) {
561 String utf8Url = null;
562 try {
563 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
564 } catch (UnsupportedEncodingException e) {
565 Log.e(TAG, "err " + e);
566 }
567 if (utf8Url != null && utf8Url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
568 return true;
569 }
570 return false;
571 }
572
573 private boolean handleMakeCallTypeUrl(String url) {
574 // wtai://wp/mc;number
575 // number=string(phone-number)
576 if (url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
577 Intent intent = new Intent(Intent.ACTION_VIEW,
578 Uri.parse(WebView.SCHEME_TEL +
579 url.substring(UrlHandler.SCHEME_WTAI_MC.length())));
580 getContext().startActivity(intent);
581 // before leaving BrowserActivity, close the empty child tab.
582 // If a new tab is created through JavaScript open to load this
583 // url, we would like to close it as we will load this url in a
584 // different Activity.
585 Tab current = mUiController.getCurrentTab();
586 if (current != null
587 && current.getWebView().copyBackForwardList().getSize() == 0) {
588 mUiController.closeCurrentTab();
589 }
590 return true;
591 }
592 return false;
593 }
594
kaiyizc4ada322013-07-30 09:58:07 +0800595 private boolean isEstoreTypeUrl(String url) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700596 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800597 return true;
598 }
599 return false;
600 }
601
Vivek Sekharb54614f2014-05-01 19:03:37 -0700602 private boolean handleEstoreTypeUrl(String url) {
603 if (url.getBytes().length > 256) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800604 Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700605 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800606 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700607
608 Intent intent;
609 // perform generic parsing of the URI to turn it into an Intent.
610 try {
611 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
612 } catch (URISyntaxException ex) {
613 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
614 return false;
615 }
616
kaiyizc4ada322013-07-30 09:58:07 +0800617 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800618 getContext().startActivity(intent);
kaiyizc4ada322013-07-30 09:58:07 +0800619 } catch (ActivityNotFoundException ex) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800620 String downloadUrl = getContext().getResources().getString(R.string.estore_homepage);
kaiyizc4ada322013-07-30 09:58:07 +0800621 mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800622 Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800623 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700624
625 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800626 }
627
kaiyiz950eca22013-08-08 11:01:28 +0800628 private boolean isRtspTypeUrl(String url) {
629 String utf8Url = null;
630 try {
631 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
632 } catch (UnsupportedEncodingException e) {
633 Log.e(TAG, "err " + e);
634 }
635 if (utf8Url != null && utf8Url.startsWith("rtsp://")) {
636 return true;
637 }
638 return false;
639 }
640
641 private boolean handleRtspTypeUrl(String url) {
642 Intent intent;
643 // perform generic parsing of the URI to turn it into an Intent.
644 try {
645 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
646 } catch (URISyntaxException ex) {
647 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
648 return false;
649 }
650
651 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800652 getContext().startActivity(intent);
kaiyiz950eca22013-08-08 11:01:28 +0800653 } catch (ActivityNotFoundException ex) {
654 Log.w("Browser", "No resolveActivity " + url);
655 return false;
656 }
657 return true;
658 }
659
John Reck0f602f32011-07-07 15:38:43 -0700660 @Override
661 public void onDismiss() {
662 final Tab currentTab = mBaseUi.getActiveTab();
663 mBaseUi.hideTitleBar();
664 post(new Runnable() {
665 public void run() {
666 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700667 if (currentTab != null) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700668 setDisplayTitle(currentTab.getTitle(), currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700669 }
670 }
671 });
672 }
673
674 /**
675 * callback from the suggestion dropdown
676 * copy text to input field and stay in edit mode
677 */
678 @Override
679 public void onCopySuggestion(String text) {
680 mUrlInput.setText(text, true);
681 if (text != null) {
682 mUrlInput.setSelection(text.length());
683 }
684 }
685
686 public void setCurrentUrlIsBookmark(boolean isBookmark) {
687 }
688
689 @Override
690 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
691 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Sagar Dhawan5a5d01f2015-07-30 17:16:21 -0700692 if (mUiController.getCurrentTab() != null &&
693 mUiController.getCurrentTab().isKeyboardShowing()){
694 stopEditingUrl();
695 return true;
696 }
John Reck0f602f32011-07-07 15:38:43 -0700697 // catch back key in order to do slightly more cleanup than usual
Michael Kolb0b129122012-06-04 16:31:58 -0700698 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700699 }
700 return super.dispatchKeyEventPreIme(evt);
701 }
702
John Reck0f602f32011-07-07 15:38:43 -0700703 /**
704 * called from the Ui when the user wants to edit
705 * @param clearInput clear the input field
706 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700707 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700708 // editing takes preference of progress
709 setVisibility(View.VISIBLE);
John Reck0f602f32011-07-07 15:38:43 -0700710 if (!mUrlInput.hasFocus()) {
711 mUrlInput.requestFocus();
712 }
713 if (clearInput) {
714 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700715 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700716 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700717 mUrlInput.showIME();
718 }
John Reck0f602f32011-07-07 15:38:43 -0700719 }
720
721 public void onProgressStarted() {
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700722 mFaviconTile.setBadgeBlockedObjectsCount(0);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700723 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNKNOWN);
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700724 mFaviconTile.setBadgeHasCertIssues(false);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700725 mFaviconTile.replaceFavicon(mDefaultFavicon);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700726 setSecurityState(Tab.SecurityState.SECURITY_STATE_NOT_SECURE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700727 mHandler.removeMessages(WEBREFINER_COUNTER_MSG);
728 mHandler.sendEmptyMessageDelayed(WEBREFINER_COUNTER_MSG,
729 WEBREFINER_COUNTER_MSG_DELAY);
730 mStopButton.setImageResource(R.drawable.ic_action_stop);
731 mStopButton.setContentDescription(getResources().
732 getString(R.string.accessibility_button_stop));
John Reck0f602f32011-07-07 15:38:43 -0700733 }
734
735 public void onProgressStopped() {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700736 if (!isEditingUrl()) {
737 mFaviconTile.setVisibility(View.VISIBLE);
738 }
739 mStopButton.setImageResource(R.drawable.ic_action_reload);
740 mStopButton.setContentDescription(getResources().
741 getString(R.string.accessibility_button_refresh));
John Reck0f602f32011-07-07 15:38:43 -0700742 }
743
John Reck419f6b42011-08-16 16:10:51 -0700744 public void onTabDataChanged(Tab tab) {
745 }
746
Michael Kolb0b129122012-06-04 16:31:58 -0700747 public void onVoiceResult(String s) {
748 startEditingUrl(true, true);
749 onCopySuggestion(s);
750 }
751
Narayan Kamathf3174a52011-11-17 14:43:32 +0000752 @Override
753 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
754
755 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700756 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000757
758 @Override
759 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700760
Enrico Ros1f5a0952014-11-18 20:15:48 -0800761 @Override
762 public void onStateChanged(int state) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700763 mVoiceButton.setVisibility(View.GONE);
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700764 mClearButton.setVisibility(View.GONE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800765 switch(state) {
766 case STATE_NORMAL:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700767 mFaviconTile.setVisibility(View.VISIBLE);
768 mMagnify.setVisibility(View.GONE);
Sagar Dhawanaff56c92015-07-13 15:57:11 -0700769 mMore.setVisibility(View.VISIBLE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700770 if (mUiController != null) {
771 Tab currentTab = mUiController.getCurrentTab();
772 if (currentTab != null){
773 if (TextUtils.isEmpty(currentTab.getUrl())) {
774 mFaviconTile.setVisibility(View.GONE);
775 mMagnify.setVisibility(View.VISIBLE);
776 }
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700777 setDisplayTitle(currentTab.getTitle(), currentTab.getUrl());
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700778 }
779 mUiController.setWindowDimming(0.0f);
780 }
781
Enrico Ros1f5a0952014-11-18 20:15:48 -0800782 break;
783 case STATE_HIGHLIGHTED:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700784 mFaviconTile.setVisibility(View.GONE);
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700785 mMagnify.setVisibility(View.VISIBLE);
786 mClearButton.setVisibility(View.VISIBLE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800787 mMore.setVisibility(View.GONE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700788 if (mUiController != null) {
789 mUiController.setWindowDimming(0.75f);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700790 if (mTrustLevel == SiteTileView.TRUST_TRUSTED) {
791 Tab currentTab = mUiController.getCurrentTab();
792 if (currentTab != null) {
793 mUrlInput.setText(currentTab.getUrl(), false);
794 mUrlInput.selectAll();
795 }
796 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700797 }
Enrico Ros1f5a0952014-11-18 20:15:48 -0800798 break;
799 case STATE_EDITED:
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700800 if (TextUtils.isEmpty(mUrlInput.getText()) &&
801 mUiController != null &&
802 mUiController.supportsVoice()) {
803 mVoiceButton.setVisibility(View.VISIBLE);
804 }
805 else {
806 mClearButton.setVisibility(View.VISIBLE);
807 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700808 mFaviconTile.setVisibility(View.GONE);
809 mMagnify.setVisibility(View.VISIBLE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800810 mMore.setVisibility(View.GONE);
811 break;
812 }
813 }
814
815 public boolean isMenuShowing() {
816 return mOverflowMenuShowing;
817 }
818
819
820 @Override
821 public void onDismiss(PopupMenu popupMenu) {
822 if (popupMenu == mPopupMenu) {
823 onMenuHidden();
824 }
825 }
826
827 private void onMenuHidden() {
828 mOverflowMenuShowing = false;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800829 }
830
831
832 @Override
833 public boolean onMenuItemClick(MenuItem item) {
834 return mUiController.onOptionsItemSelected(item);
835 }
John Reck0f602f32011-07-07 15:38:43 -0700836}