blob: 90b551b77667cb64c0fafa488418f7d4debb654d [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;
jrizzolia62318b2015-09-08 21:44:50 +020079 private View mSeparator;
Pankaj Garg66f8cef2015-07-07 17:29:03 -070080 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);
jrizzolia62318b2015-09-08 21:44:50 +0200125 mSeparator = findViewById(R.id.separator);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700126 mFaviconTile = (SiteTileView) findViewById(R.id.favicon_view);
127 mFaviconTile.setOnClickListener(this);
128 mVoiceButton = findViewById(R.id.voice);
129 mVoiceButton.setOnClickListener(this);
130 mClearButton = (ImageView) findViewById(R.id.clear);
131 mClearButton.setOnClickListener(this);
132 mStopButton = (ImageView) findViewById(R.id.stop);
133 mStopButton.setOnClickListener(this);
134
135 mDefaultFavicon = BitmapFactory.decodeResource(getResources(),
136 R.drawable.ic_deco_favicon_normal);
137
138 mHandler = new Handler() {
139 @Override
140 public void handleMessage(Message m) {
141 switch (m.what) {
142 case WEBREFINER_COUNTER_MSG:
143 WebView wv = mUiController.getCurrentTopWebView();
144 if (wv != null && WebRefiner.isInitialized()) {
145 int count = WebRefiner.getInstance().getBlockedURLCount(wv);
146 if (count > 0) {
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700147 mFaviconTile.setBadgeBlockedObjectsCount(count);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700148 }
149 }
150 mHandler.sendEmptyMessageDelayed(WEBREFINER_COUNTER_MSG,
151 WEBREFINER_COUNTER_MSG_DELAY);
152 break;
153 }
154 }
155 };
John Reck0f602f32011-07-07 15:38:43 -0700156 }
157
158 public void setTitleBar(TitleBar titleBar) {
159 mTitleBar = titleBar;
160 mBaseUi = mTitleBar.getUi();
161 mUiController = mTitleBar.getUiController();
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700162 mUrlInput.setController(mUiController);
John Reck0f602f32011-07-07 15:38:43 -0700163 }
164
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700165 public void setSecurityState(Tab.SecurityState securityState) {
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700166 switch (securityState) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700167 case SECURITY_STATE_SECURE:
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700168 mTrustLevel = SiteTileView.TRUST_TRUSTED;
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700169 mFaviconTile.setBadgeHasCertIssues(false);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700170 break;
171 case SECURITY_STATE_MIXED:
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700172 mTrustLevel = SiteTileView.TRUST_UNTRUSTED;
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700173 mFaviconTile.setBadgeHasCertIssues(true);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700174 mTitleBar.showTopControls(false);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700175 break;
176 case SECURITY_STATE_BAD_CERTIFICATE:
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700177 mTrustLevel = SiteTileView.TRUST_AVOID;
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700178 mFaviconTile.setBadgeHasCertIssues(true);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700179 mTitleBar.showTopControls(false);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700180 break;
181 case SECURITY_STATE_NOT_SECURE:
182 default:
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700183 mTrustLevel = SiteTileView.TRUST_UNKNOWN;
John Reck0f602f32011-07-07 15:38:43 -0700184 }
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700185 mFaviconTile.setTrustLevel(mTrustLevel);
186 }
187
188 public int getTrustLevel() {
189 return mTrustLevel;
John Reck0f602f32011-07-07 15:38:43 -0700190 }
191
Pankaj Garg32e1b942015-06-03 18:13:24 -0700192 public static int adjustColor(int color, float hueMultiplier,
193 float saturationMultiplier, float valueMultiplier) {
194 float[] hsv = new float[3];
195 Color.colorToHSV(color, hsv);
196 hsv[0] *= hueMultiplier;
197 hsv[1] *= saturationMultiplier;
198 hsv[2] *= valueMultiplier;
199 return Color.HSVToColor(Color.alpha(color), hsv);
200 }
201
202 public static void setStatusAndNavigationBarColor(final Activity activity, int color) {
203 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
204 int currentColor = activity.getWindow().getStatusBarColor();
205 Integer from = currentColor;
206 Integer to = color;
207 ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
208
209 if (mDefaultStatusBarColor == -1) {
210 mDefaultStatusBarColor = activity.getWindow().getStatusBarColor();
211 }
212
213 animator.addUpdateListener(
214 new ValueAnimator.AnimatorUpdateListener() {
215 @Override
216 public void onAnimationUpdate(ValueAnimator animation) {
217 Integer value = (Integer) animation.getAnimatedValue();
218 activity.getWindow().setStatusBarColor(value.intValue());
219 //activity.getWindow().setNavigationBarColor(value.intValue());
220 }
221 }
222 );
223 animator.start();
224 }
225 }
226
227 private void updateSiteIconColor(String urlString, int color) {
228 try {
229 URL url = new URL(urlString);
230 String host = url.getHost();
231 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
232 int currentColor = prefs.getInt(host + ":color", 0);
233 if (currentColor != color) {
234 SharedPreferences.Editor editor = prefs.edit();
235 editor.remove(host + ":color");
236 editor.putInt(host + ":color", color);
237 editor.commit();
238 }
239 } catch (MalformedURLException e) {
240 }
241 }
242
243 public static int getSiteIconColor(String urlString) {
244 try {
245 URL url = new URL(urlString);
246 String host = url.getHost();
247 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
248 return prefs.getInt(host + ":color", 0);
249 } catch (MalformedURLException e) {
250 return 0;
251 }
252 }
253
254 public static int getDefaultStatusBarColor() {
255 return mDefaultStatusBarColor;
256 }
257
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700258 // Sets the favicon for the given tab if it's in the foreground
259 // If the tab doesn't have a favicon, it sets the default favicon
260 public void showCurrentFavicon(Tab tab) {
261 int color;
262 if (tab == null) { return; }
Pankaj Garg32e1b942015-06-03 18:13:24 -0700263
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700264 if (tab.inForeground()) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700265 if (tab.hasFavicon()) {
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700266 color = ColorUtils.getDominantColorForBitmap(tab.getFavicon());
Pankaj Garg32e1b942015-06-03 18:13:24 -0700267 updateSiteIconColor(tab.getUrl(), color);
268 setStatusAndNavigationBarColor(mUiController.getActivity(),
269 adjustColor(color, 1, 1, 0.7f));
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700270
271 } else {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700272 color = getSiteIconColor(tab.getUrl());
273 if (color != 0) {
274 setStatusAndNavigationBarColor(mUiController.getActivity(),
275 adjustColor(color, 1, 1, 0.7f));
276 } else {
277 setStatusAndNavigationBarColor(mUiController.getActivity(),
278 mDefaultStatusBarColor);
279 }
280 }
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700281 if (mFaviconTile != null) {
282 mFaviconTile.replaceFavicon(tab.getFavicon()); // Always set the tab's favicon
283 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700284 }
John Reck0f602f32011-07-07 15:38:43 -0700285 }
286
Pankaj Garg32e1b942015-06-03 18:13:24 -0700287 protected void showSiteSpecificSettings() {
288 WebView wv = mUiController.getCurrentTopWebView();
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700289 int ads = 0;
290 int tracker = 0;
291 int malware = 0;
Pankaj Garg32e1b942015-06-03 18:13:24 -0700292
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700293 WebRefiner webRefiner = WebRefiner.getInstance();
294 if (wv != null && webRefiner != null) {
295 WebRefiner.PageInfo pageInfo = webRefiner.getPageInfo(wv);
296 if (pageInfo != null) {
297 for (WebRefiner.MatchedURLInfo urlInfo : pageInfo.mMatchedURLInfoList) {
Pankaj Gargee992032015-07-24 15:59:36 -0700298 if (urlInfo.mActionTaken == WebRefiner.MatchedURLInfo.ACTION_BLOCKED) {
299 switch (urlInfo.mMatchedFilterCategory) {
300 case WebRefiner.RuleSet.CATEGORY_ADS:
301 ads++;
302 break;
303 case WebRefiner.RuleSet.CATEGORY_TRACKERS:
304 tracker++;
305 break;
306 case WebRefiner.RuleSet.CATEGORY_MALWARE_DOMAINS:
307 malware++;
308 break;
309 }
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700310 }
311 }
312 }
Pankaj Garg32e1b942015-06-03 18:13:24 -0700313 }
314
315 Bundle bundle = new Bundle();
316 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE,
317 mUiController.getCurrentTab().getUrl());
Sagar Dhawane28023a2015-08-13 16:40:25 -0700318 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE_TITLE,
319 mUiController.getCurrentTab().getTitle());
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700320 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_ADS_INFO, ads);
321 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_TRACKER_INFO, tracker);
322 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_MALWARE_INFO, malware);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700323
324 bundle.putParcelable(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT,
325 SslCertificate.saveState(wv.getCertificate()));
326
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700327 Tab.SecurityState securityState = Tab.getWebViewSecurityState(
328 mUiController.getCurrentTab().getWebView());
329 if (securityState == Tab.SecurityState.SECURITY_STATE_MIXED) {
330 bundle.putBoolean(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_MIXED, true);
331 } else if (securityState == Tab.SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
332 bundle.putBoolean(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_BAD, true);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700333 }
334
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700335 Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
Pankaj Garg32e1b942015-06-03 18:13:24 -0700336 if (favicon != null) {
337 ByteArrayOutputStream baos = new ByteArrayOutputStream();
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700338 favicon.compress(Bitmap.CompressFormat.PNG, 100, baos);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700339 bundle.putByteArray(SiteSpecificPreferencesFragment.EXTRA_FAVICON,
340 baos.toByteArray());
341 }
342 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(
Pankaj Garg75273bb2015-08-20 11:43:49 -0700343 mUiController.getActivity(), SiteSpecificPreferencesFragment.class.getName(),
344 bundle, Controller.PREFERENCES_PAGE);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700345 }
346
John Reck0f602f32011-07-07 15:38:43 -0700347 @Override
348 public void onClick(View v) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700349 Tab currentTab = mUiController.getCurrentTab();
Vivek Sekharae3b1792015-08-03 12:26:41 -0700350 WebView wv = currentTab.getWebView();
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700351 String url = null;
352 if (currentTab != null){
353 url = currentTab.getUrl();
354 }
Enrico Ros1f5a0952014-11-18 20:15:48 -0800355 if (mMore == v) {
356 showMenu(mMore);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700357 } else if (mFaviconTile == v) {
Vivek Sekharae3b1792015-08-03 12:26:41 -0700358 if (urlHasSitePrefs(url) && (wv != null && !wv.isShowingInterstitialPage()) ){
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700359 showSiteSpecificSettings();
360 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700361 } else if (mVoiceButton == v) {
362 mUiController.startVoiceRecognizer();
363 } else if (mStopButton == v) {
364 stopOrRefresh();
365 } else if (mClearButton == v) {
366 clearOrClose();
367 mUrlInput.setText("");
368 }
369 }
370
371 private static boolean urlHasSitePrefs(String url) {
372 if (TextUtils.isEmpty(url)) {
373 return false;
374 }
375
376 for (int i = 0; i < noSitePrefs.length; i++) {
377 if (url.startsWith(noSitePrefs[i])) {
378 return false;
379 }
380 }
381 return true;
382 }
383
384 private void stopOrRefresh() {
385 if (mUiController == null) return;
386 if (mTitleBar.isInLoad()) {
387 mUiController.stopLoading();
388 } else {
389 if (mUiController.getCurrentTopWebView() != null) {
390 stopEditingUrl();
391 mUiController.getCurrentTopWebView().reload();
392 }
393 }
394 }
395
396 private void clearOrClose() {
397 if (TextUtils.isEmpty(mUrlInput.getText())) {
398 // close
399 mUrlInput.clearFocus();
400 } else {
401 // clear
402 mUrlInput.setText("");
Enrico Ros1f5a0952014-11-18 20:15:48 -0800403 }
404 }
405
406 void showMenu(View anchor) {
407 Activity activity = mUiController.getActivity();
408 if (mPopupMenu == null) {
409 mPopupMenu = new PopupMenu(getContext(), anchor);
410 mPopupMenu.setOnMenuItemClickListener(this);
411 mPopupMenu.setOnDismissListener(this);
412 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
413 mPopupMenu = null;
414 return;
415 }
416 }
417 Menu menu = mPopupMenu.getMenu();
418
419 if (mUiController instanceof Controller) {
420 Controller controller = (Controller) mUiController;
421 if (controller.onPrepareOptionsMenu(menu)) {
422 mOverflowMenuShowing = true;
423 }
424 }
John Reck0f602f32011-07-07 15:38:43 -0700425 }
426
427 @Override
428 public void onFocusChange(View view, boolean hasFocus) {
429 // if losing focus and not in touch mode, leave as is
430 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
431 setFocusState(hasFocus);
432 }
433 if (hasFocus) {
434 mBaseUi.showTitleBar();
Dany Rybnikovbcd37da2015-04-20 11:43:15 +0300435 if (!BrowserSettings.getInstance().isPowerSaveModeEnabled()) {
436 //Notify about anticipated network activity
437 NetworkServices.hintUpcomingUserActivity();
438 }
John Reck0f602f32011-07-07 15:38:43 -0700439 } else if (!mUrlInput.needsUpdate()) {
440 mUrlInput.dismissDropDown();
441 mUrlInput.hideIME();
442 if (mUrlInput.getText().length() == 0) {
443 Tab currentTab = mUiController.getTabControl().getCurrentTab();
444 if (currentTab != null) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700445 setDisplayTitle(currentTab.getTitle(), currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700446 }
447 }
John Reck0f602f32011-07-07 15:38:43 -0700448 }
449 mUrlInput.clearNeedsUpdate();
450 }
451
452 protected void setFocusState(boolean focus) {
453 }
454
John Reck0f602f32011-07-07 15:38:43 -0700455 public boolean isEditingUrl() {
456 return mUrlInput.hasFocus();
457 }
458
459 void stopEditingUrl() {
Michael Kolb0b129122012-06-04 16:31:58 -0700460 WebView currentTopWebView = mUiController.getCurrentTopWebView();
461 if (currentTopWebView != null) {
462 currentTopWebView.requestFocus();
463 }
John Reck0f602f32011-07-07 15:38:43 -0700464 }
465
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700466 void setDisplayTitle(String title, String url) {
John Reck0f602f32011-07-07 15:38:43 -0700467 if (!isEditingUrl()) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700468 if (!TextUtils.isEmpty(title)) {
469 if (mTrustLevel == SiteTileView.TRUST_TRUSTED) {
470 if (!title.equals(mUrlInput.getText().toString())) {
471 mUrlInput.setText(title, false);
472 }
473 return;
474 }
475 }
476 if (!url.equals(mUrlInput.getText().toString())) {
477 mUrlInput.setText(url, false);
Michael Kolb29aab402012-05-29 17:22:35 -0700478 }
John Reck0f602f32011-07-07 15:38:43 -0700479 }
480 }
481
John Reck0f602f32011-07-07 15:38:43 -0700482 void setIncognitoMode(boolean incognito) {
483 mUrlInput.setIncognitoMode(incognito);
484 }
485
486 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000487 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700488 }
489
490 // UrlInputListener implementation
491
492 /**
493 * callback from suggestion dropdown
494 * user selected a suggestion
495 */
496 @Override
497 public void onAction(String text, String extra, String source) {
Michael Kolb0b129122012-06-04 16:31:58 -0700498 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700499 if (UrlInputView.TYPED.equals(source)) {
kaiyizc4ada322013-07-30 09:58:07 +0800500 String url = null;
Panos Thomas4bdb5252014-11-13 16:20:11 -0800501 boolean wap2estore = BrowserConfig.getInstance(getContext())
502 .hasFeature(BrowserConfig.Feature.WAP2ESTORE);
Vivek Sekhar5e631472014-03-31 23:59:10 -0700503 if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text)
504 || isMakeCallTypeUrl(text)) {
kaiyizc4ada322013-07-30 09:58:07 +0800505 url = text;
506 } else {
507 url = UrlUtils.smartUrlFilter(text, false);
508 }
509
John Reck0f602f32011-07-07 15:38:43 -0700510 Tab t = mBaseUi.getActiveTab();
511 // Only shortcut javascript URIs for now, as there is special
512 // logic in UrlHandler for other schemas
Axesh R. Ajmera6fc73692015-08-11 16:32:10 -0700513 if (url != null && t != null && url.startsWith("javascript:")) {
John Reck0f602f32011-07-07 15:38:43 -0700514 mUiController.loadUrl(t, url);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700515 setDisplayTitle(null, text);
John Reck0f602f32011-07-07 15:38:43 -0700516 return;
517 }
kaiyizc4ada322013-07-30 09:58:07 +0800518
519 // add for carrier wap2estore feature
520 if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700521 if (handleEstoreTypeUrl(url)) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700522 setDisplayTitle(null, text);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700523 return;
524 }
kaiyizc4ada322013-07-30 09:58:07 +0800525 }
kaiyiz950eca22013-08-08 11:01:28 +0800526 // add for rtsp scheme feature
527 if (url != null && t != null && isRtspTypeUrl(url)) {
528 if (handleRtspTypeUrl(url)) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700529 setDisplayTitle(null, text);
kaiyiz950eca22013-08-08 11:01:28 +0800530 return;
531 }
532 }
Vivek Sekhar5e631472014-03-31 23:59:10 -0700533 // add for "wtai://wp/mc;" scheme feature
534 if (url != null && t != null && isMakeCallTypeUrl(url)) {
535 if (handleMakeCallTypeUrl(url)) {
536 return;
537 }
538 }
John Reck0f602f32011-07-07 15:38:43 -0700539 }
540 Intent i = new Intent();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700541 String action = Intent.ACTION_SEARCH;
John Reck0f602f32011-07-07 15:38:43 -0700542 i.setAction(action);
543 i.putExtra(SearchManager.QUERY, text);
544 if (extra != null) {
545 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
546 }
547 if (source != null) {
548 Bundle appData = new Bundle();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800549 appData.putString("source", source);
550 i.putExtra("source", appData);
John Reck0f602f32011-07-07 15:38:43 -0700551 }
552 mUiController.handleNewIntent(i);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700553 setDisplayTitle(null, text);
John Reck0f602f32011-07-07 15:38:43 -0700554 }
555
Vivek Sekhar5e631472014-03-31 23:59:10 -0700556 private boolean isMakeCallTypeUrl(String url) {
557 String utf8Url = null;
558 try {
559 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
560 } catch (UnsupportedEncodingException e) {
561 Log.e(TAG, "err " + e);
562 }
563 if (utf8Url != null && utf8Url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
564 return true;
565 }
566 return false;
567 }
568
569 private boolean handleMakeCallTypeUrl(String url) {
570 // wtai://wp/mc;number
571 // number=string(phone-number)
572 if (url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
573 Intent intent = new Intent(Intent.ACTION_VIEW,
574 Uri.parse(WebView.SCHEME_TEL +
575 url.substring(UrlHandler.SCHEME_WTAI_MC.length())));
576 getContext().startActivity(intent);
577 // before leaving BrowserActivity, close the empty child tab.
578 // If a new tab is created through JavaScript open to load this
579 // url, we would like to close it as we will load this url in a
580 // different Activity.
581 Tab current = mUiController.getCurrentTab();
582 if (current != null
583 && current.getWebView().copyBackForwardList().getSize() == 0) {
584 mUiController.closeCurrentTab();
585 }
586 return true;
587 }
588 return false;
589 }
590
kaiyizc4ada322013-07-30 09:58:07 +0800591 private boolean isEstoreTypeUrl(String url) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700592 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800593 return true;
594 }
595 return false;
596 }
597
Vivek Sekharb54614f2014-05-01 19:03:37 -0700598 private boolean handleEstoreTypeUrl(String url) {
599 if (url.getBytes().length > 256) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800600 Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700601 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800602 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700603
604 Intent intent;
605 // perform generic parsing of the URI to turn it into an Intent.
606 try {
607 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
608 } catch (URISyntaxException ex) {
609 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
610 return false;
611 }
612
kaiyizc4ada322013-07-30 09:58:07 +0800613 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800614 getContext().startActivity(intent);
kaiyizc4ada322013-07-30 09:58:07 +0800615 } catch (ActivityNotFoundException ex) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800616 String downloadUrl = getContext().getResources().getString(R.string.estore_homepage);
kaiyizc4ada322013-07-30 09:58:07 +0800617 mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800618 Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800619 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700620
621 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800622 }
623
kaiyiz950eca22013-08-08 11:01:28 +0800624 private boolean isRtspTypeUrl(String url) {
625 String utf8Url = null;
626 try {
627 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
628 } catch (UnsupportedEncodingException e) {
629 Log.e(TAG, "err " + e);
630 }
631 if (utf8Url != null && utf8Url.startsWith("rtsp://")) {
632 return true;
633 }
634 return false;
635 }
636
637 private boolean handleRtspTypeUrl(String url) {
638 Intent intent;
639 // perform generic parsing of the URI to turn it into an Intent.
640 try {
641 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
642 } catch (URISyntaxException ex) {
643 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
644 return false;
645 }
646
647 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800648 getContext().startActivity(intent);
kaiyiz950eca22013-08-08 11:01:28 +0800649 } catch (ActivityNotFoundException ex) {
650 Log.w("Browser", "No resolveActivity " + url);
651 return false;
652 }
653 return true;
654 }
655
John Reck0f602f32011-07-07 15:38:43 -0700656 @Override
657 public void onDismiss() {
658 final Tab currentTab = mBaseUi.getActiveTab();
659 mBaseUi.hideTitleBar();
660 post(new Runnable() {
661 public void run() {
662 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700663 if (currentTab != null) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700664 setDisplayTitle(currentTab.getTitle(), currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700665 }
666 }
667 });
668 }
669
670 /**
671 * callback from the suggestion dropdown
672 * copy text to input field and stay in edit mode
673 */
674 @Override
675 public void onCopySuggestion(String text) {
676 mUrlInput.setText(text, true);
John Reck0f602f32011-07-07 15:38:43 -0700677 }
678
679 public void setCurrentUrlIsBookmark(boolean isBookmark) {
680 }
681
682 @Override
683 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
684 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Sagar Dhawan5a5d01f2015-07-30 17:16:21 -0700685 if (mUiController.getCurrentTab() != null &&
686 mUiController.getCurrentTab().isKeyboardShowing()){
687 stopEditingUrl();
688 return true;
689 }
John Reck0f602f32011-07-07 15:38:43 -0700690 // catch back key in order to do slightly more cleanup than usual
Michael Kolb0b129122012-06-04 16:31:58 -0700691 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700692 }
693 return super.dispatchKeyEventPreIme(evt);
694 }
695
John Reck0f602f32011-07-07 15:38:43 -0700696 /**
697 * called from the Ui when the user wants to edit
698 * @param clearInput clear the input field
699 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700700 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700701 // editing takes preference of progress
702 setVisibility(View.VISIBLE);
John Reck0f602f32011-07-07 15:38:43 -0700703 if (clearInput) {
704 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700705 }
Tarun Nainaniaa586822015-08-20 14:59:33 -0700706 if (!mUrlInput.hasFocus()) {
707 mUrlInput.requestFocus();
708 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700709 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700710 mUrlInput.showIME();
711 }
John Reck0f602f32011-07-07 15:38:43 -0700712 }
713
714 public void onProgressStarted() {
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700715 mFaviconTile.setBadgeBlockedObjectsCount(0);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700716 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNKNOWN);
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700717 mFaviconTile.setBadgeHasCertIssues(false);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700718 setSecurityState(Tab.SecurityState.SECURITY_STATE_NOT_SECURE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700719 mHandler.removeMessages(WEBREFINER_COUNTER_MSG);
720 mHandler.sendEmptyMessageDelayed(WEBREFINER_COUNTER_MSG,
721 WEBREFINER_COUNTER_MSG_DELAY);
722 mStopButton.setImageResource(R.drawable.ic_action_stop);
723 mStopButton.setContentDescription(getResources().
724 getString(R.string.accessibility_button_stop));
John Reck0f602f32011-07-07 15:38:43 -0700725 }
726
727 public void onProgressStopped() {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700728 if (!isEditingUrl()) {
729 mFaviconTile.setVisibility(View.VISIBLE);
730 }
731 mStopButton.setImageResource(R.drawable.ic_action_reload);
732 mStopButton.setContentDescription(getResources().
733 getString(R.string.accessibility_button_refresh));
John Reck0f602f32011-07-07 15:38:43 -0700734 }
735
John Reck419f6b42011-08-16 16:10:51 -0700736 public void onTabDataChanged(Tab tab) {
737 }
738
Michael Kolb0b129122012-06-04 16:31:58 -0700739 public void onVoiceResult(String s) {
740 startEditingUrl(true, true);
741 onCopySuggestion(s);
742 }
743
Narayan Kamathf3174a52011-11-17 14:43:32 +0000744 @Override
745 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
746
747 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700748 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000749
750 @Override
751 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700752
Enrico Ros1f5a0952014-11-18 20:15:48 -0800753 @Override
754 public void onStateChanged(int state) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700755 mVoiceButton.setVisibility(View.GONE);
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700756 mClearButton.setVisibility(View.GONE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800757 switch(state) {
758 case STATE_NORMAL:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700759 mFaviconTile.setVisibility(View.VISIBLE);
jrizzolia62318b2015-09-08 21:44:50 +0200760 mSeparator.setVisibility(View.VISIBLE);
Sagar Dhawanaff56c92015-07-13 15:57:11 -0700761 mMore.setVisibility(View.VISIBLE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700762 if (mUiController != null) {
763 Tab currentTab = mUiController.getCurrentTab();
764 if (currentTab != null){
765 if (TextUtils.isEmpty(currentTab.getUrl())) {
766 mFaviconTile.setVisibility(View.GONE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700767 }
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700768 setDisplayTitle(currentTab.getTitle(), currentTab.getUrl());
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700769 }
770 mUiController.setWindowDimming(0.0f);
771 }
772
Enrico Ros1f5a0952014-11-18 20:15:48 -0800773 break;
774 case STATE_HIGHLIGHTED:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700775 mFaviconTile.setVisibility(View.GONE);
jrizzolia62318b2015-09-08 21:44:50 +0200776 mSeparator.setVisibility(View.GONE);
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700777 mClearButton.setVisibility(View.VISIBLE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800778 mMore.setVisibility(View.GONE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700779 if (mUiController != null) {
780 mUiController.setWindowDimming(0.75f);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700781 if (mTrustLevel == SiteTileView.TRUST_TRUSTED) {
782 Tab currentTab = mUiController.getCurrentTab();
783 if (currentTab != null) {
784 mUrlInput.setText(currentTab.getUrl(), false);
785 mUrlInput.selectAll();
786 }
787 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700788 }
Enrico Ros1f5a0952014-11-18 20:15:48 -0800789 break;
790 case STATE_EDITED:
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700791 if (TextUtils.isEmpty(mUrlInput.getText()) &&
792 mUiController != null &&
793 mUiController.supportsVoice()) {
794 mVoiceButton.setVisibility(View.VISIBLE);
795 }
796 else {
797 mClearButton.setVisibility(View.VISIBLE);
798 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700799 mFaviconTile.setVisibility(View.GONE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800800 mMore.setVisibility(View.GONE);
801 break;
802 }
803 }
804
805 public boolean isMenuShowing() {
806 return mOverflowMenuShowing;
807 }
808
809
810 @Override
811 public void onDismiss(PopupMenu popupMenu) {
812 if (popupMenu == mPopupMenu) {
813 onMenuHidden();
814 }
815 }
816
817 private void onMenuHidden() {
818 mOverflowMenuShowing = false;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800819 }
820
821
822 @Override
823 public boolean onMenuItemClick(MenuItem item) {
824 return mUiController.onOptionsItemSelected(item);
825 }
John Reck0f602f32011-07-07 15:38:43 -0700826}