blob: 690b007949e51c9ed4e2938c6283e0a96dd87f99 [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) {
Sagar Dhawaneecefa32015-09-25 12:02:34 -0700203 // The flag that allows coloring is disabled in PowerSaveMode, no point in trying to color.
204 if (BrowserSettings.getInstance().isPowerSaveModeEnabled())
205 return;
Pankaj Garg32e1b942015-06-03 18:13:24 -0700206 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
207 int currentColor = activity.getWindow().getStatusBarColor();
208 Integer from = currentColor;
209 Integer to = color;
210 ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
211
212 if (mDefaultStatusBarColor == -1) {
213 mDefaultStatusBarColor = activity.getWindow().getStatusBarColor();
214 }
215
216 animator.addUpdateListener(
217 new ValueAnimator.AnimatorUpdateListener() {
218 @Override
219 public void onAnimationUpdate(ValueAnimator animation) {
220 Integer value = (Integer) animation.getAnimatedValue();
221 activity.getWindow().setStatusBarColor(value.intValue());
222 //activity.getWindow().setNavigationBarColor(value.intValue());
223 }
224 }
225 );
226 animator.start();
227 }
228 }
229
230 private void updateSiteIconColor(String urlString, int color) {
231 try {
232 URL url = new URL(urlString);
233 String host = url.getHost();
234 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
235 int currentColor = prefs.getInt(host + ":color", 0);
236 if (currentColor != color) {
237 SharedPreferences.Editor editor = prefs.edit();
238 editor.remove(host + ":color");
239 editor.putInt(host + ":color", color);
240 editor.commit();
241 }
242 } catch (MalformedURLException e) {
243 }
244 }
245
246 public static int getSiteIconColor(String urlString) {
247 try {
248 URL url = new URL(urlString);
249 String host = url.getHost();
250 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
251 return prefs.getInt(host + ":color", 0);
252 } catch (MalformedURLException e) {
253 return 0;
254 }
255 }
256
257 public static int getDefaultStatusBarColor() {
258 return mDefaultStatusBarColor;
259 }
260
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700261 // Sets the favicon for the given tab if it's in the foreground
262 // If the tab doesn't have a favicon, it sets the default favicon
263 public void showCurrentFavicon(Tab tab) {
264 int color;
265 if (tab == null) { return; }
Pankaj Garg32e1b942015-06-03 18:13:24 -0700266
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700267 if (tab.inForeground()) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700268 if (tab.hasFavicon()) {
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700269 color = ColorUtils.getDominantColorForBitmap(tab.getFavicon());
Pankaj Garg32e1b942015-06-03 18:13:24 -0700270 updateSiteIconColor(tab.getUrl(), color);
271 setStatusAndNavigationBarColor(mUiController.getActivity(),
272 adjustColor(color, 1, 1, 0.7f));
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700273
274 } else {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700275 color = getSiteIconColor(tab.getUrl());
276 if (color != 0) {
277 setStatusAndNavigationBarColor(mUiController.getActivity(),
278 adjustColor(color, 1, 1, 0.7f));
279 } else {
280 setStatusAndNavigationBarColor(mUiController.getActivity(),
281 mDefaultStatusBarColor);
282 }
283 }
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700284 if (mFaviconTile != null) {
285 mFaviconTile.replaceFavicon(tab.getFavicon()); // Always set the tab's favicon
286 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700287 }
John Reck0f602f32011-07-07 15:38:43 -0700288 }
289
Pankaj Garg32e1b942015-06-03 18:13:24 -0700290 protected void showSiteSpecificSettings() {
291 WebView wv = mUiController.getCurrentTopWebView();
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700292 int ads = 0;
293 int tracker = 0;
294 int malware = 0;
Pankaj Garg32e1b942015-06-03 18:13:24 -0700295
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700296 WebRefiner webRefiner = WebRefiner.getInstance();
297 if (wv != null && webRefiner != null) {
298 WebRefiner.PageInfo pageInfo = webRefiner.getPageInfo(wv);
299 if (pageInfo != null) {
300 for (WebRefiner.MatchedURLInfo urlInfo : pageInfo.mMatchedURLInfoList) {
Pankaj Gargee992032015-07-24 15:59:36 -0700301 if (urlInfo.mActionTaken == WebRefiner.MatchedURLInfo.ACTION_BLOCKED) {
302 switch (urlInfo.mMatchedFilterCategory) {
303 case WebRefiner.RuleSet.CATEGORY_ADS:
304 ads++;
305 break;
306 case WebRefiner.RuleSet.CATEGORY_TRACKERS:
307 tracker++;
308 break;
309 case WebRefiner.RuleSet.CATEGORY_MALWARE_DOMAINS:
310 malware++;
311 break;
312 }
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700313 }
314 }
315 }
Pankaj Garg32e1b942015-06-03 18:13:24 -0700316 }
317
318 Bundle bundle = new Bundle();
319 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE,
320 mUiController.getCurrentTab().getUrl());
Sagar Dhawane28023a2015-08-13 16:40:25 -0700321 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE_TITLE,
322 mUiController.getCurrentTab().getTitle());
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700323 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_ADS_INFO, ads);
324 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_TRACKER_INFO, tracker);
325 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_MALWARE_INFO, malware);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700326
327 bundle.putParcelable(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT,
328 SslCertificate.saveState(wv.getCertificate()));
329
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700330 Tab.SecurityState securityState = Tab.getWebViewSecurityState(
331 mUiController.getCurrentTab().getWebView());
332 if (securityState == Tab.SecurityState.SECURITY_STATE_MIXED) {
333 bundle.putBoolean(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_MIXED, true);
334 } else if (securityState == Tab.SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
335 bundle.putBoolean(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_BAD, true);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700336 }
337
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700338 Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
Pankaj Garg32e1b942015-06-03 18:13:24 -0700339 if (favicon != null) {
340 ByteArrayOutputStream baos = new ByteArrayOutputStream();
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700341 favicon.compress(Bitmap.CompressFormat.PNG, 100, baos);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700342 bundle.putByteArray(SiteSpecificPreferencesFragment.EXTRA_FAVICON,
343 baos.toByteArray());
344 }
345 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(
Pankaj Garg75273bb2015-08-20 11:43:49 -0700346 mUiController.getActivity(), SiteSpecificPreferencesFragment.class.getName(),
347 bundle, Controller.PREFERENCES_PAGE);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700348 }
349
John Reck0f602f32011-07-07 15:38:43 -0700350 @Override
351 public void onClick(View v) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700352 Tab currentTab = mUiController.getCurrentTab();
353 String url = null;
Sagar Dhawan20b1deb2015-10-12 11:13:18 -0700354 WebView wv = null;
355 if (currentTab != null) {
356 wv = currentTab.getWebView();
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700357 url = currentTab.getUrl();
358 }
Sagar Dhawan20b1deb2015-10-12 11:13:18 -0700359
Enrico Ros1f5a0952014-11-18 20:15:48 -0800360 if (mMore == v) {
361 showMenu(mMore);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700362 } else if (mFaviconTile == v) {
Vivek Sekharae3b1792015-08-03 12:26:41 -0700363 if (urlHasSitePrefs(url) && (wv != null && !wv.isShowingInterstitialPage()) ){
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700364 showSiteSpecificSettings();
365 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700366 } else if (mVoiceButton == v) {
367 mUiController.startVoiceRecognizer();
368 } else if (mStopButton == v) {
369 stopOrRefresh();
370 } else if (mClearButton == v) {
371 clearOrClose();
372 mUrlInput.setText("");
373 }
374 }
375
376 private static boolean urlHasSitePrefs(String url) {
377 if (TextUtils.isEmpty(url)) {
378 return false;
379 }
380
381 for (int i = 0; i < noSitePrefs.length; i++) {
382 if (url.startsWith(noSitePrefs[i])) {
383 return false;
384 }
385 }
386 return true;
387 }
388
389 private void stopOrRefresh() {
390 if (mUiController == null) return;
391 if (mTitleBar.isInLoad()) {
392 mUiController.stopLoading();
393 } else {
394 if (mUiController.getCurrentTopWebView() != null) {
395 stopEditingUrl();
396 mUiController.getCurrentTopWebView().reload();
397 }
398 }
399 }
400
401 private void clearOrClose() {
402 if (TextUtils.isEmpty(mUrlInput.getText())) {
403 // close
404 mUrlInput.clearFocus();
405 } else {
406 // clear
407 mUrlInput.setText("");
Enrico Ros1f5a0952014-11-18 20:15:48 -0800408 }
409 }
410
411 void showMenu(View anchor) {
412 Activity activity = mUiController.getActivity();
413 if (mPopupMenu == null) {
414 mPopupMenu = new PopupMenu(getContext(), anchor);
415 mPopupMenu.setOnMenuItemClickListener(this);
416 mPopupMenu.setOnDismissListener(this);
417 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
418 mPopupMenu = null;
419 return;
420 }
421 }
422 Menu menu = mPopupMenu.getMenu();
423
424 if (mUiController instanceof Controller) {
425 Controller controller = (Controller) mUiController;
426 if (controller.onPrepareOptionsMenu(menu)) {
427 mOverflowMenuShowing = true;
428 }
429 }
John Reck0f602f32011-07-07 15:38:43 -0700430 }
431
432 @Override
433 public void onFocusChange(View view, boolean hasFocus) {
434 // if losing focus and not in touch mode, leave as is
435 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
436 setFocusState(hasFocus);
437 }
438 if (hasFocus) {
439 mBaseUi.showTitleBar();
Dany Rybnikovbcd37da2015-04-20 11:43:15 +0300440 if (!BrowserSettings.getInstance().isPowerSaveModeEnabled()) {
441 //Notify about anticipated network activity
442 NetworkServices.hintUpcomingUserActivity();
443 }
John Reck0f602f32011-07-07 15:38:43 -0700444 } else if (!mUrlInput.needsUpdate()) {
445 mUrlInput.dismissDropDown();
446 mUrlInput.hideIME();
447 if (mUrlInput.getText().length() == 0) {
448 Tab currentTab = mUiController.getTabControl().getCurrentTab();
449 if (currentTab != null) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700450 setDisplayTitle(currentTab.getTitle(), currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700451 }
452 }
John Reck0f602f32011-07-07 15:38:43 -0700453 }
454 mUrlInput.clearNeedsUpdate();
455 }
456
457 protected void setFocusState(boolean focus) {
458 }
459
John Reck0f602f32011-07-07 15:38:43 -0700460 public boolean isEditingUrl() {
461 return mUrlInput.hasFocus();
462 }
463
464 void stopEditingUrl() {
Michael Kolb0b129122012-06-04 16:31:58 -0700465 WebView currentTopWebView = mUiController.getCurrentTopWebView();
466 if (currentTopWebView != null) {
467 currentTopWebView.requestFocus();
468 }
John Reck0f602f32011-07-07 15:38:43 -0700469 }
470
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700471 void setDisplayTitle(String title, String url) {
John Reck0f602f32011-07-07 15:38:43 -0700472 if (!isEditingUrl()) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700473 if (!TextUtils.isEmpty(title)) {
474 if (mTrustLevel == SiteTileView.TRUST_TRUSTED) {
475 if (!title.equals(mUrlInput.getText().toString())) {
476 mUrlInput.setText(title, false);
477 }
478 return;
479 }
480 }
481 if (!url.equals(mUrlInput.getText().toString())) {
482 mUrlInput.setText(url, false);
Michael Kolb29aab402012-05-29 17:22:35 -0700483 }
John Reck0f602f32011-07-07 15:38:43 -0700484 }
485 }
486
John Reck0f602f32011-07-07 15:38:43 -0700487 void setIncognitoMode(boolean incognito) {
488 mUrlInput.setIncognitoMode(incognito);
489 }
490
491 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000492 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700493 }
494
495 // UrlInputListener implementation
496
497 /**
498 * callback from suggestion dropdown
499 * user selected a suggestion
500 */
501 @Override
502 public void onAction(String text, String extra, String source) {
Michael Kolb0b129122012-06-04 16:31:58 -0700503 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700504 if (UrlInputView.TYPED.equals(source)) {
kaiyizc4ada322013-07-30 09:58:07 +0800505 String url = null;
Panos Thomas4bdb5252014-11-13 16:20:11 -0800506 boolean wap2estore = BrowserConfig.getInstance(getContext())
507 .hasFeature(BrowserConfig.Feature.WAP2ESTORE);
Vivek Sekhar5e631472014-03-31 23:59:10 -0700508 if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text)
509 || isMakeCallTypeUrl(text)) {
kaiyizc4ada322013-07-30 09:58:07 +0800510 url = text;
511 } else {
512 url = UrlUtils.smartUrlFilter(text, false);
513 }
514
John Reck0f602f32011-07-07 15:38:43 -0700515 Tab t = mBaseUi.getActiveTab();
516 // Only shortcut javascript URIs for now, as there is special
517 // logic in UrlHandler for other schemas
Axesh R. Ajmera6fc73692015-08-11 16:32:10 -0700518 if (url != null && t != null && url.startsWith("javascript:")) {
John Reck0f602f32011-07-07 15:38:43 -0700519 mUiController.loadUrl(t, url);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700520 setDisplayTitle(null, text);
John Reck0f602f32011-07-07 15:38:43 -0700521 return;
522 }
kaiyizc4ada322013-07-30 09:58:07 +0800523
524 // add for carrier wap2estore feature
525 if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700526 if (handleEstoreTypeUrl(url)) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700527 setDisplayTitle(null, text);
Vivek Sekharb54614f2014-05-01 19:03:37 -0700528 return;
529 }
kaiyizc4ada322013-07-30 09:58:07 +0800530 }
kaiyiz950eca22013-08-08 11:01:28 +0800531 // add for rtsp scheme feature
532 if (url != null && t != null && isRtspTypeUrl(url)) {
533 if (handleRtspTypeUrl(url)) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700534 setDisplayTitle(null, text);
kaiyiz950eca22013-08-08 11:01:28 +0800535 return;
536 }
537 }
Vivek Sekhar5e631472014-03-31 23:59:10 -0700538 // add for "wtai://wp/mc;" scheme feature
539 if (url != null && t != null && isMakeCallTypeUrl(url)) {
540 if (handleMakeCallTypeUrl(url)) {
541 return;
542 }
543 }
John Reck0f602f32011-07-07 15:38:43 -0700544 }
545 Intent i = new Intent();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700546 String action = Intent.ACTION_SEARCH;
John Reck0f602f32011-07-07 15:38:43 -0700547 i.setAction(action);
548 i.putExtra(SearchManager.QUERY, text);
549 if (extra != null) {
550 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
551 }
552 if (source != null) {
553 Bundle appData = new Bundle();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800554 appData.putString("source", source);
555 i.putExtra("source", appData);
John Reck0f602f32011-07-07 15:38:43 -0700556 }
557 mUiController.handleNewIntent(i);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700558 setDisplayTitle(null, text);
John Reck0f602f32011-07-07 15:38:43 -0700559 }
560
Vivek Sekhar5e631472014-03-31 23:59:10 -0700561 private boolean isMakeCallTypeUrl(String url) {
562 String utf8Url = null;
563 try {
564 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
565 } catch (UnsupportedEncodingException e) {
566 Log.e(TAG, "err " + e);
567 }
568 if (utf8Url != null && utf8Url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
569 return true;
570 }
571 return false;
572 }
573
574 private boolean handleMakeCallTypeUrl(String url) {
575 // wtai://wp/mc;number
576 // number=string(phone-number)
577 if (url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
578 Intent intent = new Intent(Intent.ACTION_VIEW,
579 Uri.parse(WebView.SCHEME_TEL +
580 url.substring(UrlHandler.SCHEME_WTAI_MC.length())));
581 getContext().startActivity(intent);
582 // before leaving BrowserActivity, close the empty child tab.
583 // If a new tab is created through JavaScript open to load this
584 // url, we would like to close it as we will load this url in a
585 // different Activity.
586 Tab current = mUiController.getCurrentTab();
587 if (current != null
588 && current.getWebView().copyBackForwardList().getSize() == 0) {
589 mUiController.closeCurrentTab();
590 }
591 return true;
592 }
593 return false;
594 }
595
kaiyizc4ada322013-07-30 09:58:07 +0800596 private boolean isEstoreTypeUrl(String url) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700597 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800598 return true;
599 }
600 return false;
601 }
602
Vivek Sekharb54614f2014-05-01 19:03:37 -0700603 private boolean handleEstoreTypeUrl(String url) {
604 if (url.getBytes().length > 256) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800605 Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700606 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800607 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700608
609 Intent intent;
610 // perform generic parsing of the URI to turn it into an Intent.
611 try {
612 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
613 } catch (URISyntaxException ex) {
614 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
615 return false;
616 }
617
kaiyizc4ada322013-07-30 09:58:07 +0800618 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800619 getContext().startActivity(intent);
kaiyizc4ada322013-07-30 09:58:07 +0800620 } catch (ActivityNotFoundException ex) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800621 String downloadUrl = getContext().getResources().getString(R.string.estore_homepage);
kaiyizc4ada322013-07-30 09:58:07 +0800622 mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800623 Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800624 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700625
626 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800627 }
628
kaiyiz950eca22013-08-08 11:01:28 +0800629 private boolean isRtspTypeUrl(String url) {
630 String utf8Url = null;
631 try {
632 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
633 } catch (UnsupportedEncodingException e) {
634 Log.e(TAG, "err " + e);
635 }
636 if (utf8Url != null && utf8Url.startsWith("rtsp://")) {
637 return true;
638 }
639 return false;
640 }
641
642 private boolean handleRtspTypeUrl(String url) {
643 Intent intent;
644 // perform generic parsing of the URI to turn it into an Intent.
645 try {
646 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
647 } catch (URISyntaxException ex) {
648 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
649 return false;
650 }
651
652 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800653 getContext().startActivity(intent);
kaiyiz950eca22013-08-08 11:01:28 +0800654 } catch (ActivityNotFoundException ex) {
655 Log.w("Browser", "No resolveActivity " + url);
656 return false;
657 }
658 return true;
659 }
660
John Reck0f602f32011-07-07 15:38:43 -0700661 @Override
662 public void onDismiss() {
663 final Tab currentTab = mBaseUi.getActiveTab();
664 mBaseUi.hideTitleBar();
665 post(new Runnable() {
666 public void run() {
667 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700668 if (currentTab != null) {
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700669 setDisplayTitle(currentTab.getTitle(), currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700670 }
671 }
672 });
673 }
674
675 /**
676 * callback from the suggestion dropdown
677 * copy text to input field and stay in edit mode
678 */
679 @Override
680 public void onCopySuggestion(String text) {
681 mUrlInput.setText(text, true);
John Reck0f602f32011-07-07 15:38:43 -0700682 }
683
684 public void setCurrentUrlIsBookmark(boolean isBookmark) {
685 }
686
687 @Override
688 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
689 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Sagar Dhawan5a5d01f2015-07-30 17:16:21 -0700690 if (mUiController.getCurrentTab() != null &&
691 mUiController.getCurrentTab().isKeyboardShowing()){
692 stopEditingUrl();
693 return true;
694 }
John Reck0f602f32011-07-07 15:38:43 -0700695 // catch back key in order to do slightly more cleanup than usual
Michael Kolb0b129122012-06-04 16:31:58 -0700696 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700697 }
698 return super.dispatchKeyEventPreIme(evt);
699 }
700
John Reck0f602f32011-07-07 15:38:43 -0700701 /**
702 * called from the Ui when the user wants to edit
703 * @param clearInput clear the input field
704 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700705 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700706 // editing takes preference of progress
707 setVisibility(View.VISIBLE);
John Reck0f602f32011-07-07 15:38:43 -0700708 if (clearInput) {
709 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700710 }
Tarun Nainaniaa586822015-08-20 14:59:33 -0700711 if (!mUrlInput.hasFocus()) {
712 mUrlInput.requestFocus();
713 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700714 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700715 mUrlInput.showIME();
716 }
John Reck0f602f32011-07-07 15:38:43 -0700717 }
718
719 public void onProgressStarted() {
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700720 mFaviconTile.setBadgeBlockedObjectsCount(0);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700721 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNKNOWN);
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700722 mFaviconTile.setBadgeHasCertIssues(false);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700723 setSecurityState(Tab.SecurityState.SECURITY_STATE_NOT_SECURE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700724 mHandler.removeMessages(WEBREFINER_COUNTER_MSG);
725 mHandler.sendEmptyMessageDelayed(WEBREFINER_COUNTER_MSG,
726 WEBREFINER_COUNTER_MSG_DELAY);
727 mStopButton.setImageResource(R.drawable.ic_action_stop);
728 mStopButton.setContentDescription(getResources().
729 getString(R.string.accessibility_button_stop));
John Reck0f602f32011-07-07 15:38:43 -0700730 }
731
732 public void onProgressStopped() {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700733 if (!isEditingUrl()) {
734 mFaviconTile.setVisibility(View.VISIBLE);
735 }
736 mStopButton.setImageResource(R.drawable.ic_action_reload);
737 mStopButton.setContentDescription(getResources().
738 getString(R.string.accessibility_button_refresh));
John Reck0f602f32011-07-07 15:38:43 -0700739 }
740
John Reck419f6b42011-08-16 16:10:51 -0700741 public void onTabDataChanged(Tab tab) {
742 }
743
Michael Kolb0b129122012-06-04 16:31:58 -0700744 public void onVoiceResult(String s) {
745 startEditingUrl(true, true);
746 onCopySuggestion(s);
747 }
748
Narayan Kamathf3174a52011-11-17 14:43:32 +0000749 @Override
750 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
751
752 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700753 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000754
755 @Override
756 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700757
Enrico Ros1f5a0952014-11-18 20:15:48 -0800758 @Override
759 public void onStateChanged(int state) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700760 mVoiceButton.setVisibility(View.GONE);
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700761 mClearButton.setVisibility(View.GONE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800762 switch(state) {
763 case STATE_NORMAL:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700764 mFaviconTile.setVisibility(View.VISIBLE);
jrizzolia62318b2015-09-08 21:44:50 +0200765 mSeparator.setVisibility(View.VISIBLE);
Sagar Dhawanaff56c92015-07-13 15:57:11 -0700766 mMore.setVisibility(View.VISIBLE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700767 if (mUiController != null) {
768 Tab currentTab = mUiController.getCurrentTab();
769 if (currentTab != null){
770 if (TextUtils.isEmpty(currentTab.getUrl())) {
771 mFaviconTile.setVisibility(View.GONE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700772 }
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700773 setDisplayTitle(currentTab.getTitle(), currentTab.getUrl());
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700774 }
775 mUiController.setWindowDimming(0.0f);
776 }
777
Enrico Ros1f5a0952014-11-18 20:15:48 -0800778 break;
779 case STATE_HIGHLIGHTED:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700780 mFaviconTile.setVisibility(View.GONE);
jrizzolia62318b2015-09-08 21:44:50 +0200781 mSeparator.setVisibility(View.GONE);
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700782 mClearButton.setVisibility(View.VISIBLE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800783 mMore.setVisibility(View.GONE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700784 if (mUiController != null) {
785 mUiController.setWindowDimming(0.75f);
Pankaj Garg8d2e98f2015-08-07 10:01:49 -0700786 if (mTrustLevel == SiteTileView.TRUST_TRUSTED) {
787 Tab currentTab = mUiController.getCurrentTab();
788 if (currentTab != null) {
789 mUrlInput.setText(currentTab.getUrl(), false);
790 mUrlInput.selectAll();
791 }
792 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700793 }
Enrico Ros1f5a0952014-11-18 20:15:48 -0800794 break;
795 case STATE_EDITED:
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700796 if (TextUtils.isEmpty(mUrlInput.getText()) &&
797 mUiController != null &&
798 mUiController.supportsVoice()) {
799 mVoiceButton.setVisibility(View.VISIBLE);
800 }
801 else {
802 mClearButton.setVisibility(View.VISIBLE);
803 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700804 mFaviconTile.setVisibility(View.GONE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800805 mMore.setVisibility(View.GONE);
806 break;
807 }
808 }
809
810 public boolean isMenuShowing() {
811 return mOverflowMenuShowing;
812 }
813
814
815 @Override
816 public void onDismiss(PopupMenu popupMenu) {
817 if (popupMenu == mPopupMenu) {
818 onMenuHidden();
819 }
820 }
821
822 private void onMenuHidden() {
823 mOverflowMenuShowing = false;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800824 }
825
826
827 @Override
828 public boolean onMenuItemClick(MenuItem item) {
829 return mUiController.onOptionsItemSelected(item);
830 }
John Reck0f602f32011-07-07 15:38:43 -0700831}