blob: ce65f16c10794a414e774ddf67568e2b5507d074 [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;
Kevin Hart055e6d82014-12-19 15:53:19 -080061import org.codeaurora.swe.Engine;
Pankaj Garg32e1b942015-06-03 18:13:24 -070062import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080063import org.codeaurora.swe.WebView;
Pankaj Garg32e1b942015-06-03 18:13:24 -070064import org.codeaurora.swe.util.ColorUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080065
John Reck42229bc2011-08-19 13:26:43 -070066public class NavigationBarBase extends LinearLayout implements
67 OnClickListener, UrlInputListener, OnFocusChangeListener,
Enrico Ros1f5a0952014-11-18 20:15:48 -080068 TextWatcher, UrlInputView.StateListener,
69 PopupMenu.OnMenuItemClickListener, PopupMenu.OnDismissListener {
John Reck0f602f32011-07-07 15:38:43 -070070
kaiyizc4ada322013-07-30 09:58:07 +080071 private final static String TAG = "NavigationBarBase";
72
John Reck0f602f32011-07-07 15:38:43 -070073 protected BaseUi mBaseUi;
74 protected TitleBar mTitleBar;
75 protected UiController mUiController;
76 protected UrlInputView mUrlInput;
Pankaj Garg66f8cef2015-07-07 17:29:03 -070077 protected ImageView mStopButton;
John Reck0f602f32011-07-07 15:38:43 -070078
Pankaj Garg66f8cef2015-07-07 17:29:03 -070079 private SiteTileView mFaviconTile;
80 private ImageView mMagnify;
81 private View mVoiceButton;
82 private ImageView mClearButton;
Enrico Ros1f5a0952014-11-18 20:15:48 -080083 private View mMore;
84 private PopupMenu mPopupMenu;
85 private boolean mOverflowMenuShowing;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070086
Pankaj Garg66f8cef2015-07-07 17:29:03 -070087 private static Bitmap mDefaultFavicon;
88
Pankaj Garg32e1b942015-06-03 18:13:24 -070089 private int mStatusBarColor;
90 private static int mDefaultStatusBarColor = -1;
91
Pankaj Garg66f8cef2015-07-07 17:29:03 -070092 private static final int WEBREFINER_COUNTER_MSG = 4242;
93 private static final int WEBREFINER_COUNTER_MSG_DELAY = 3000;
94 private Handler mHandler;
95
Pankaj Garg66f8cef2015-07-07 17:29:03 -070096 private static final String noSitePrefs[] = {
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -070097 "browser://",
Pankaj Garg66f8cef2015-07-07 17:29:03 -070098 "about:",
99 "content:",
100 };
101
John Reck0f602f32011-07-07 15:38:43 -0700102 public NavigationBarBase(Context context) {
103 super(context);
104 }
105
106 public NavigationBarBase(Context context, AttributeSet attrs) {
107 super(context, attrs);
108 }
109
110 public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) {
111 super(context, attrs, defStyle);
112 }
113
114 @Override
115 protected void onFinishInflate() {
116 super.onFinishInflate();
John Reck0f602f32011-07-07 15:38:43 -0700117 mUrlInput = (UrlInputView) findViewById(R.id.url);
118 mUrlInput.setUrlInputListener(this);
John Reck0f602f32011-07-07 15:38:43 -0700119 mUrlInput.setOnFocusChangeListener(this);
120 mUrlInput.setSelectAllOnFocus(true);
Narayan Kamathf3174a52011-11-17 14:43:32 +0000121 mUrlInput.addTextChangedListener(this);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800122 mMore = findViewById(R.id.more_browser_settings);
123 mMore.setOnClickListener(this);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700124 mMagnify = (ImageView) findViewById(R.id.magnify);
125 mMagnify.setOnClickListener(this);
126 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:
168 mFaviconTile.setTrustLevel(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:
172 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNTRUSTED);
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700173 mFaviconTile.setBadgeHasCertIssues(true);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700174 break;
175 case SECURITY_STATE_BAD_CERTIFICATE:
176 mFaviconTile.setTrustLevel(SiteTileView.TRUST_AVOID);
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700177 mFaviconTile.setBadgeHasCertIssues(true);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700178 break;
179 case SECURITY_STATE_NOT_SECURE:
180 default:
181 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNKNOWN);
John Reck0f602f32011-07-07 15:38:43 -0700182 }
183 }
184
Pankaj Garg32e1b942015-06-03 18:13:24 -0700185 public static int adjustColor(int color, float hueMultiplier,
186 float saturationMultiplier, float valueMultiplier) {
187 float[] hsv = new float[3];
188 Color.colorToHSV(color, hsv);
189 hsv[0] *= hueMultiplier;
190 hsv[1] *= saturationMultiplier;
191 hsv[2] *= valueMultiplier;
192 return Color.HSVToColor(Color.alpha(color), hsv);
193 }
194
195 public static void setStatusAndNavigationBarColor(final Activity activity, int color) {
196 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
197 int currentColor = activity.getWindow().getStatusBarColor();
198 Integer from = currentColor;
199 Integer to = color;
200 ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
201
202 if (mDefaultStatusBarColor == -1) {
203 mDefaultStatusBarColor = activity.getWindow().getStatusBarColor();
204 }
205
206 animator.addUpdateListener(
207 new ValueAnimator.AnimatorUpdateListener() {
208 @Override
209 public void onAnimationUpdate(ValueAnimator animation) {
210 Integer value = (Integer) animation.getAnimatedValue();
211 activity.getWindow().setStatusBarColor(value.intValue());
212 //activity.getWindow().setNavigationBarColor(value.intValue());
213 }
214 }
215 );
216 animator.start();
217 }
218 }
219
220 private void updateSiteIconColor(String urlString, int color) {
221 try {
222 URL url = new URL(urlString);
223 String host = url.getHost();
224 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
225 int currentColor = prefs.getInt(host + ":color", 0);
226 if (currentColor != color) {
227 SharedPreferences.Editor editor = prefs.edit();
228 editor.remove(host + ":color");
229 editor.putInt(host + ":color", color);
230 editor.commit();
231 }
232 } catch (MalformedURLException e) {
233 }
234 }
235
236 public static int getSiteIconColor(String urlString) {
237 try {
238 URL url = new URL(urlString);
239 String host = url.getHost();
240 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
241 return prefs.getInt(host + ":color", 0);
242 } catch (MalformedURLException e) {
243 return 0;
244 }
245 }
246
247 public static int getDefaultStatusBarColor() {
248 return mDefaultStatusBarColor;
249 }
250
John Reck0f602f32011-07-07 15:38:43 -0700251 public void setFavicon(Bitmap icon) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700252 int color = ColorUtils.getDominantColorForBitmap(icon);
253 Tab tab = mUiController.getCurrentTab();
254
255 if (tab != null) {
256 if (tab.hasFavicon()) {
257 updateSiteIconColor(tab.getUrl(), color);
258 setStatusAndNavigationBarColor(mUiController.getActivity(),
259 adjustColor(color, 1, 1, 0.7f));
260 } else {
261 color = getSiteIconColor(tab.getUrl());
262 if (color != 0) {
263 setStatusAndNavigationBarColor(mUiController.getActivity(),
264 adjustColor(color, 1, 1, 0.7f));
265 } else {
266 setStatusAndNavigationBarColor(mUiController.getActivity(),
267 mDefaultStatusBarColor);
268 }
269 }
270 } else {
271 setStatusAndNavigationBarColor(mUiController.getActivity(), mDefaultStatusBarColor);
272 }
273
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700274 //Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
275
276 if (mFaviconTile != null) {
277 mFaviconTile.replaceFavicon(mUiController.getCurrentTopWebView().getFavicon());
278 }
John Reck0f602f32011-07-07 15:38:43 -0700279 }
280
Pankaj Garg32e1b942015-06-03 18:13:24 -0700281 protected void showSiteSpecificSettings() {
282 WebView wv = mUiController.getCurrentTopWebView();
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700283 int ads = 0;
284 int tracker = 0;
285 int malware = 0;
Pankaj Garg32e1b942015-06-03 18:13:24 -0700286
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700287 WebRefiner webRefiner = WebRefiner.getInstance();
288 if (wv != null && webRefiner != null) {
289 WebRefiner.PageInfo pageInfo = webRefiner.getPageInfo(wv);
290 if (pageInfo != null) {
291 for (WebRefiner.MatchedURLInfo urlInfo : pageInfo.mMatchedURLInfoList) {
Pankaj Gargee992032015-07-24 15:59:36 -0700292 if (urlInfo.mActionTaken == WebRefiner.MatchedURLInfo.ACTION_BLOCKED) {
293 switch (urlInfo.mMatchedFilterCategory) {
294 case WebRefiner.RuleSet.CATEGORY_ADS:
295 ads++;
296 break;
297 case WebRefiner.RuleSet.CATEGORY_TRACKERS:
298 tracker++;
299 break;
300 case WebRefiner.RuleSet.CATEGORY_MALWARE_DOMAINS:
301 malware++;
302 break;
303 }
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700304 }
305 }
306 }
Pankaj Garg32e1b942015-06-03 18:13:24 -0700307 }
308
309 Bundle bundle = new Bundle();
310 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE,
311 mUiController.getCurrentTab().getUrl());
Pankaj Gargdbdf5aa2015-07-16 15:22:37 -0700312
313 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_ADS_INFO, ads);
314 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_TRACKER_INFO, tracker);
315 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_MALWARE_INFO, malware);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700316
317 bundle.putParcelable(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT,
318 SslCertificate.saveState(wv.getCertificate()));
319
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700320 Tab.SecurityState securityState = Tab.getWebViewSecurityState(
321 mUiController.getCurrentTab().getWebView());
322 if (securityState == Tab.SecurityState.SECURITY_STATE_MIXED) {
323 bundle.putBoolean(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_MIXED, true);
324 } else if (securityState == Tab.SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
325 bundle.putBoolean(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_BAD, true);
Pankaj Garg32e1b942015-06-03 18:13:24 -0700326 }
327
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700328 Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
Pankaj Garg32e1b942015-06-03 18:13:24 -0700329 if (favicon != null) {
330 ByteArrayOutputStream baos = new ByteArrayOutputStream();
331 favicon.compress(Bitmap.CompressFormat.PNG, 50, baos);
332 bundle.putByteArray(SiteSpecificPreferencesFragment.EXTRA_FAVICON,
333 baos.toByteArray());
334 }
335 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(
336 mUiController.getActivity(),
337 SiteSpecificPreferencesFragment.class.getName(), bundle, 0);
338 }
339
John Reck0f602f32011-07-07 15:38:43 -0700340 @Override
341 public void onClick(View v) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700342 Tab currentTab = mUiController.getCurrentTab();
343 String url = null;
344 if (currentTab != null){
345 url = currentTab.getUrl();
346 }
Enrico Ros1f5a0952014-11-18 20:15:48 -0800347 if (mMore == v) {
348 showMenu(mMore);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700349 } else if (mFaviconTile == v) {
350 if (urlHasSitePrefs(url)){
351 showSiteSpecificSettings();
352 }
353 } else if (mMagnify == v) {
354 startEditingUrl(true, true);
355 } else if (mVoiceButton == v) {
356 mUiController.startVoiceRecognizer();
357 } else if (mStopButton == v) {
358 stopOrRefresh();
359 } else if (mClearButton == v) {
360 clearOrClose();
361 mUrlInput.setText("");
362 }
363 }
364
365 private static boolean urlHasSitePrefs(String url) {
366 if (TextUtils.isEmpty(url)) {
367 return false;
368 }
369
370 for (int i = 0; i < noSitePrefs.length; i++) {
371 if (url.startsWith(noSitePrefs[i])) {
372 return false;
373 }
374 }
375 return true;
376 }
377
378 private void stopOrRefresh() {
379 if (mUiController == null) return;
380 if (mTitleBar.isInLoad()) {
381 mUiController.stopLoading();
382 } else {
383 if (mUiController.getCurrentTopWebView() != null) {
384 stopEditingUrl();
385 mUiController.getCurrentTopWebView().reload();
386 }
387 }
388 }
389
390 private void clearOrClose() {
391 if (TextUtils.isEmpty(mUrlInput.getText())) {
392 // close
393 mUrlInput.clearFocus();
394 } else {
395 // clear
396 mUrlInput.setText("");
Enrico Ros1f5a0952014-11-18 20:15:48 -0800397 }
398 }
399
400 void showMenu(View anchor) {
401 Activity activity = mUiController.getActivity();
402 if (mPopupMenu == null) {
403 mPopupMenu = new PopupMenu(getContext(), anchor);
404 mPopupMenu.setOnMenuItemClickListener(this);
405 mPopupMenu.setOnDismissListener(this);
406 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
407 mPopupMenu = null;
408 return;
409 }
410 }
411 Menu menu = mPopupMenu.getMenu();
412
413 if (mUiController instanceof Controller) {
414 Controller controller = (Controller) mUiController;
415 if (controller.onPrepareOptionsMenu(menu)) {
416 mOverflowMenuShowing = true;
417 }
418 }
John Reck0f602f32011-07-07 15:38:43 -0700419 }
420
421 @Override
422 public void onFocusChange(View view, boolean hasFocus) {
423 // if losing focus and not in touch mode, leave as is
424 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
425 setFocusState(hasFocus);
426 }
427 if (hasFocus) {
Kevin Hart055e6d82014-12-19 15:53:19 -0800428 Engine.warmUpChildProcessAsync(mUiController.getActivity().getApplicationContext());
John Reck0f602f32011-07-07 15:38:43 -0700429 mBaseUi.showTitleBar();
Dany Rybnikovbcd37da2015-04-20 11:43:15 +0300430 if (!BrowserSettings.getInstance().isPowerSaveModeEnabled()) {
431 //Notify about anticipated network activity
432 NetworkServices.hintUpcomingUserActivity();
433 }
John Reck0f602f32011-07-07 15:38:43 -0700434 } else if (!mUrlInput.needsUpdate()) {
435 mUrlInput.dismissDropDown();
436 mUrlInput.hideIME();
437 if (mUrlInput.getText().length() == 0) {
438 Tab currentTab = mUiController.getTabControl().getCurrentTab();
439 if (currentTab != null) {
John Reck434e9f82011-08-10 18:16:52 -0700440 setDisplayTitle(currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700441 }
442 }
John Reck0f602f32011-07-07 15:38:43 -0700443 }
444 mUrlInput.clearNeedsUpdate();
445 }
446
447 protected void setFocusState(boolean focus) {
448 }
449
John Reck0f602f32011-07-07 15:38:43 -0700450 public boolean isEditingUrl() {
451 return mUrlInput.hasFocus();
452 }
453
454 void stopEditingUrl() {
Michael Kolb0b129122012-06-04 16:31:58 -0700455 WebView currentTopWebView = mUiController.getCurrentTopWebView();
456 if (currentTopWebView != null) {
457 currentTopWebView.requestFocus();
458 }
John Reck0f602f32011-07-07 15:38:43 -0700459 }
460
461 void setDisplayTitle(String title) {
462 if (!isEditingUrl()) {
Michael Kolb29aab402012-05-29 17:22:35 -0700463 if (!title.equals(mUrlInput.getText().toString())) {
464 mUrlInput.setText(title, false);
465 }
John Reck0f602f32011-07-07 15:38:43 -0700466 }
467 }
468
John Reck0f602f32011-07-07 15:38:43 -0700469 void setIncognitoMode(boolean incognito) {
470 mUrlInput.setIncognitoMode(incognito);
471 }
472
473 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000474 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700475 }
476
477 // UrlInputListener implementation
478
479 /**
480 * callback from suggestion dropdown
481 * user selected a suggestion
482 */
483 @Override
484 public void onAction(String text, String extra, String source) {
Michael Kolb0b129122012-06-04 16:31:58 -0700485 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700486 if (UrlInputView.TYPED.equals(source)) {
kaiyizc4ada322013-07-30 09:58:07 +0800487 String url = null;
Panos Thomas4bdb5252014-11-13 16:20:11 -0800488 boolean wap2estore = BrowserConfig.getInstance(getContext())
489 .hasFeature(BrowserConfig.Feature.WAP2ESTORE);
Vivek Sekhar5e631472014-03-31 23:59:10 -0700490 if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text)
491 || isMakeCallTypeUrl(text)) {
kaiyizc4ada322013-07-30 09:58:07 +0800492 url = text;
493 } else {
494 url = UrlUtils.smartUrlFilter(text, false);
495 }
496
John Reck0f602f32011-07-07 15:38:43 -0700497 Tab t = mBaseUi.getActiveTab();
498 // Only shortcut javascript URIs for now, as there is special
499 // logic in UrlHandler for other schemas
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -0700500 if (url != null && t != null && url.startsWith("javascript:")) {
John Reck0f602f32011-07-07 15:38:43 -0700501 mUiController.loadUrl(t, url);
502 setDisplayTitle(text);
503 return;
504 }
kaiyizc4ada322013-07-30 09:58:07 +0800505
506 // add for carrier wap2estore feature
507 if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700508 if (handleEstoreTypeUrl(url)) {
509 setDisplayTitle(text);
510 return;
511 }
kaiyizc4ada322013-07-30 09:58:07 +0800512 }
kaiyiz950eca22013-08-08 11:01:28 +0800513 // add for rtsp scheme feature
514 if (url != null && t != null && isRtspTypeUrl(url)) {
515 if (handleRtspTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700516 setDisplayTitle(text);
kaiyiz950eca22013-08-08 11:01:28 +0800517 return;
518 }
519 }
Vivek Sekhar5e631472014-03-31 23:59:10 -0700520 // add for "wtai://wp/mc;" scheme feature
521 if (url != null && t != null && isMakeCallTypeUrl(url)) {
522 if (handleMakeCallTypeUrl(url)) {
523 return;
524 }
525 }
John Reck0f602f32011-07-07 15:38:43 -0700526 }
527 Intent i = new Intent();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700528 String action = Intent.ACTION_SEARCH;
John Reck0f602f32011-07-07 15:38:43 -0700529 i.setAction(action);
530 i.putExtra(SearchManager.QUERY, text);
531 if (extra != null) {
532 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
533 }
534 if (source != null) {
535 Bundle appData = new Bundle();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800536 appData.putString("source", source);
537 i.putExtra("source", appData);
John Reck0f602f32011-07-07 15:38:43 -0700538 }
539 mUiController.handleNewIntent(i);
540 setDisplayTitle(text);
541 }
542
Vivek Sekhar5e631472014-03-31 23:59:10 -0700543 private boolean isMakeCallTypeUrl(String url) {
544 String utf8Url = null;
545 try {
546 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
547 } catch (UnsupportedEncodingException e) {
548 Log.e(TAG, "err " + e);
549 }
550 if (utf8Url != null && utf8Url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
551 return true;
552 }
553 return false;
554 }
555
556 private boolean handleMakeCallTypeUrl(String url) {
557 // wtai://wp/mc;number
558 // number=string(phone-number)
559 if (url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
560 Intent intent = new Intent(Intent.ACTION_VIEW,
561 Uri.parse(WebView.SCHEME_TEL +
562 url.substring(UrlHandler.SCHEME_WTAI_MC.length())));
563 getContext().startActivity(intent);
564 // before leaving BrowserActivity, close the empty child tab.
565 // If a new tab is created through JavaScript open to load this
566 // url, we would like to close it as we will load this url in a
567 // different Activity.
568 Tab current = mUiController.getCurrentTab();
569 if (current != null
570 && current.getWebView().copyBackForwardList().getSize() == 0) {
571 mUiController.closeCurrentTab();
572 }
573 return true;
574 }
575 return false;
576 }
577
kaiyizc4ada322013-07-30 09:58:07 +0800578 private boolean isEstoreTypeUrl(String url) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700579 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800580 return true;
581 }
582 return false;
583 }
584
Vivek Sekharb54614f2014-05-01 19:03:37 -0700585 private boolean handleEstoreTypeUrl(String url) {
586 if (url.getBytes().length > 256) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800587 Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700588 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800589 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700590
591 Intent intent;
592 // perform generic parsing of the URI to turn it into an Intent.
593 try {
594 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
595 } catch (URISyntaxException ex) {
596 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
597 return false;
598 }
599
kaiyizc4ada322013-07-30 09:58:07 +0800600 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800601 getContext().startActivity(intent);
kaiyizc4ada322013-07-30 09:58:07 +0800602 } catch (ActivityNotFoundException ex) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800603 String downloadUrl = getContext().getResources().getString(R.string.estore_homepage);
kaiyizc4ada322013-07-30 09:58:07 +0800604 mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800605 Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800606 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700607
608 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800609 }
610
kaiyiz950eca22013-08-08 11:01:28 +0800611 private boolean isRtspTypeUrl(String url) {
612 String utf8Url = null;
613 try {
614 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
615 } catch (UnsupportedEncodingException e) {
616 Log.e(TAG, "err " + e);
617 }
618 if (utf8Url != null && utf8Url.startsWith("rtsp://")) {
619 return true;
620 }
621 return false;
622 }
623
624 private boolean handleRtspTypeUrl(String url) {
625 Intent intent;
626 // perform generic parsing of the URI to turn it into an Intent.
627 try {
628 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
629 } catch (URISyntaxException ex) {
630 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
631 return false;
632 }
633
634 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800635 getContext().startActivity(intent);
kaiyiz950eca22013-08-08 11:01:28 +0800636 } catch (ActivityNotFoundException ex) {
637 Log.w("Browser", "No resolveActivity " + url);
638 return false;
639 }
640 return true;
641 }
642
John Reck0f602f32011-07-07 15:38:43 -0700643 @Override
644 public void onDismiss() {
645 final Tab currentTab = mBaseUi.getActiveTab();
646 mBaseUi.hideTitleBar();
647 post(new Runnable() {
648 public void run() {
649 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700650 if (currentTab != null) {
John Reck0f602f32011-07-07 15:38:43 -0700651 setDisplayTitle(currentTab.getUrl());
652 }
653 }
654 });
655 }
656
657 /**
658 * callback from the suggestion dropdown
659 * copy text to input field and stay in edit mode
660 */
661 @Override
662 public void onCopySuggestion(String text) {
663 mUrlInput.setText(text, true);
664 if (text != null) {
665 mUrlInput.setSelection(text.length());
666 }
667 }
668
669 public void setCurrentUrlIsBookmark(boolean isBookmark) {
670 }
671
672 @Override
673 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
674 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Sagar Dhawan5a5d01f2015-07-30 17:16:21 -0700675 if (mUiController.getCurrentTab() != null &&
676 mUiController.getCurrentTab().isKeyboardShowing()){
677 stopEditingUrl();
678 return true;
679 }
John Reck0f602f32011-07-07 15:38:43 -0700680 // catch back key in order to do slightly more cleanup than usual
Michael Kolb0b129122012-06-04 16:31:58 -0700681 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700682 }
683 return super.dispatchKeyEventPreIme(evt);
684 }
685
John Reck0f602f32011-07-07 15:38:43 -0700686 /**
687 * called from the Ui when the user wants to edit
688 * @param clearInput clear the input field
689 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700690 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700691 // editing takes preference of progress
692 setVisibility(View.VISIBLE);
John Reck0f602f32011-07-07 15:38:43 -0700693 if (!mUrlInput.hasFocus()) {
694 mUrlInput.requestFocus();
695 }
696 if (clearInput) {
697 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700698 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700699 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700700 mUrlInput.showIME();
701 }
John Reck0f602f32011-07-07 15:38:43 -0700702 }
703
704 public void onProgressStarted() {
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700705 mFaviconTile.setBadgeBlockedObjectsCount(0);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700706 mFaviconTile.setTrustLevel(SiteTileView.TRUST_UNKNOWN);
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700707 mFaviconTile.setBadgeHasCertIssues(false);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700708 mFaviconTile.replaceFavicon(mDefaultFavicon);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700709 setSecurityState(Tab.SecurityState.SECURITY_STATE_NOT_SECURE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700710 mHandler.removeMessages(WEBREFINER_COUNTER_MSG);
711 mHandler.sendEmptyMessageDelayed(WEBREFINER_COUNTER_MSG,
712 WEBREFINER_COUNTER_MSG_DELAY);
713 mStopButton.setImageResource(R.drawable.ic_action_stop);
714 mStopButton.setContentDescription(getResources().
715 getString(R.string.accessibility_button_stop));
John Reck0f602f32011-07-07 15:38:43 -0700716 }
717
718 public void onProgressStopped() {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700719 if (!isEditingUrl()) {
720 mFaviconTile.setVisibility(View.VISIBLE);
721 }
722 mStopButton.setImageResource(R.drawable.ic_action_reload);
723 mStopButton.setContentDescription(getResources().
724 getString(R.string.accessibility_button_refresh));
John Reck0f602f32011-07-07 15:38:43 -0700725 }
726
John Reck419f6b42011-08-16 16:10:51 -0700727 public void onTabDataChanged(Tab tab) {
728 }
729
Michael Kolb0b129122012-06-04 16:31:58 -0700730 public void onVoiceResult(String s) {
731 startEditingUrl(true, true);
732 onCopySuggestion(s);
733 }
734
Narayan Kamathf3174a52011-11-17 14:43:32 +0000735 @Override
736 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
737
738 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700739 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000740
741 @Override
742 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700743
Enrico Ros1f5a0952014-11-18 20:15:48 -0800744 @Override
745 public void onStateChanged(int state) {
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700746 mVoiceButton.setVisibility(View.GONE);
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700747 mClearButton.setVisibility(View.GONE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800748 switch(state) {
749 case STATE_NORMAL:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700750 mFaviconTile.setVisibility(View.VISIBLE);
751 mMagnify.setVisibility(View.GONE);
Sagar Dhawanaff56c92015-07-13 15:57:11 -0700752 mMore.setVisibility(View.VISIBLE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700753 if (mUiController != null) {
754 Tab currentTab = mUiController.getCurrentTab();
755 if (currentTab != null){
756 if (TextUtils.isEmpty(currentTab.getUrl())) {
757 mFaviconTile.setVisibility(View.GONE);
758 mMagnify.setVisibility(View.VISIBLE);
759 }
760 }
761 mUiController.setWindowDimming(0.0f);
762 }
763
Enrico Ros1f5a0952014-11-18 20:15:48 -0800764 break;
765 case STATE_HIGHLIGHTED:
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700766 mFaviconTile.setVisibility(View.GONE);
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700767 mMagnify.setVisibility(View.VISIBLE);
768 mClearButton.setVisibility(View.VISIBLE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800769 mMore.setVisibility(View.GONE);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700770 if (mUiController != null) {
771 mUiController.setWindowDimming(0.75f);
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700772 }
Enrico Ros1f5a0952014-11-18 20:15:48 -0800773 break;
774 case STATE_EDITED:
Sagar Dhawan84ef4442015-07-29 12:08:10 -0700775 if (TextUtils.isEmpty(mUrlInput.getText()) &&
776 mUiController != null &&
777 mUiController.supportsVoice()) {
778 mVoiceButton.setVisibility(View.VISIBLE);
779 }
780 else {
781 mClearButton.setVisibility(View.VISIBLE);
782 }
Pankaj Garg66f8cef2015-07-07 17:29:03 -0700783 mFaviconTile.setVisibility(View.GONE);
784 mMagnify.setVisibility(View.VISIBLE);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800785 mMore.setVisibility(View.GONE);
786 break;
787 }
788 }
789
790 public boolean isMenuShowing() {
791 return mOverflowMenuShowing;
792 }
793
794
795 @Override
796 public void onDismiss(PopupMenu popupMenu) {
797 if (popupMenu == mPopupMenu) {
798 onMenuHidden();
799 }
800 }
801
802 private void onMenuHidden() {
803 mOverflowMenuShowing = false;
Enrico Ros1f5a0952014-11-18 20:15:48 -0800804 }
805
806
807 @Override
808 public boolean onMenuItemClick(MenuItem item) {
809 return mUiController.onOptionsItemSelected(item);
810 }
John Reck0f602f32011-07-07 15:38:43 -0700811}