blob: 753e3626aa117f97e498232949d48e8754f4f281 [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 Garg32e1b942015-06-03 18:13:24 -070027import android.graphics.Color;
John Reck0f602f32011-07-07 15:38:43 -070028import android.graphics.drawable.Drawable;
kaiyizc4ada322013-07-30 09:58:07 +080029import android.net.Uri;
Pankaj Garg32e1b942015-06-03 18:13:24 -070030import android.net.http.SslCertificate;
31import android.net.http.SslError;
32import android.os.Build;
John Reck0f602f32011-07-07 15:38:43 -070033import android.os.Bundle;
Narayan Kamathf3174a52011-11-17 14:43:32 +000034import android.text.Editable;
35import android.text.TextWatcher;
John Reck0f602f32011-07-07 15:38:43 -070036import android.util.AttributeSet;
kaiyizc4ada322013-07-30 09:58:07 +080037import android.util.Log;
John Reck0f602f32011-07-07 15:38:43 -070038import android.view.KeyEvent;
Enrico Ros1f5a0952014-11-18 20:15:48 -080039import android.view.Menu;
40import android.view.MenuItem;
John Reck0f602f32011-07-07 15:38:43 -070041import android.view.View;
42import android.view.View.OnClickListener;
43import android.view.View.OnFocusChangeListener;
Enrico Ros1f5a0952014-11-18 20:15:48 -080044import android.view.ViewConfiguration;
John Reck0f602f32011-07-07 15:38:43 -070045import android.widget.ImageView;
46import android.widget.LinearLayout;
Enrico Ros1f5a0952014-11-18 20:15:48 -080047import android.widget.PopupMenu;
kaiyizc4ada322013-07-30 09:58:07 +080048import android.widget.Toast;
John Reck0f602f32011-07-07 15:38:43 -070049
Bijan Amirzada41242f22014-03-21 12:12:18 -070050import com.android.browser.UrlInputView.UrlInputListener;
Pankaj Garg32e1b942015-06-03 18:13:24 -070051import com.android.browser.preferences.SiteSpecificPreferencesFragment;
John Reck0f602f32011-07-07 15:38:43 -070052
Pankaj Garg32e1b942015-06-03 18:13:24 -070053import java.io.ByteArrayOutputStream;
kaiyizc4ada322013-07-30 09:58:07 +080054import java.io.UnsupportedEncodingException;
Pankaj Garg32e1b942015-06-03 18:13:24 -070055import java.net.MalformedURLException;
kaiyiz950eca22013-08-08 11:01:28 +080056import java.net.URISyntaxException;
Pankaj Garg32e1b942015-06-03 18:13:24 -070057import java.net.URL;
kaiyizc4ada322013-07-30 09:58:07 +080058
Kevin Hart055e6d82014-12-19 15:53:19 -080059import org.codeaurora.swe.Engine;
Pankaj Garg32e1b942015-06-03 18:13:24 -070060import org.codeaurora.swe.WebRefiner;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080061import org.codeaurora.swe.WebView;
Pankaj Garg32e1b942015-06-03 18:13:24 -070062import org.codeaurora.swe.util.ColorUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080063
John Reck42229bc2011-08-19 13:26:43 -070064public class NavigationBarBase extends LinearLayout implements
65 OnClickListener, UrlInputListener, OnFocusChangeListener,
Enrico Ros1f5a0952014-11-18 20:15:48 -080066 TextWatcher, UrlInputView.StateListener,
67 PopupMenu.OnMenuItemClickListener, PopupMenu.OnDismissListener {
John Reck0f602f32011-07-07 15:38:43 -070068
kaiyizc4ada322013-07-30 09:58:07 +080069 private final static String TAG = "NavigationBarBase";
70
John Reck0f602f32011-07-07 15:38:43 -070071 protected BaseUi mBaseUi;
72 protected TitleBar mTitleBar;
73 protected UiController mUiController;
74 protected UrlInputView mUrlInput;
John Reck0f602f32011-07-07 15:38:43 -070075
Pankaj Garg32e1b942015-06-03 18:13:24 -070076 protected ImageView mFaviconBadge;
77
John Reck0f602f32011-07-07 15:38:43 -070078 private ImageView mFavicon;
79 private ImageView mLockIcon;
John Reck0f602f32011-07-07 15:38:43 -070080
Enrico Ros1f5a0952014-11-18 20:15:48 -080081 private View mMore;
82 private PopupMenu mPopupMenu;
83 private boolean mOverflowMenuShowing;
84 private boolean mNeedsMenu;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -070085
Pankaj Garg32e1b942015-06-03 18:13:24 -070086 private int mStatusBarColor;
87 private static int mDefaultStatusBarColor = -1;
88
89 private Tab.SecurityState mSecurityState = Tab.SecurityState.SECURITY_STATE_NOT_SECURE;
90
John Reck0f602f32011-07-07 15:38:43 -070091 public NavigationBarBase(Context context) {
92 super(context);
93 }
94
95 public NavigationBarBase(Context context, AttributeSet attrs) {
96 super(context, attrs);
97 }
98
99 public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) {
100 super(context, attrs, defStyle);
101 }
102
103 @Override
104 protected void onFinishInflate() {
105 super.onFinishInflate();
106 mLockIcon = (ImageView) findViewById(R.id.lock);
107 mFavicon = (ImageView) findViewById(R.id.favicon);
108 mUrlInput = (UrlInputView) findViewById(R.id.url);
109 mUrlInput.setUrlInputListener(this);
John Reck0f602f32011-07-07 15:38:43 -0700110 mUrlInput.setOnFocusChangeListener(this);
111 mUrlInput.setSelectAllOnFocus(true);
Narayan Kamathf3174a52011-11-17 14:43:32 +0000112 mUrlInput.addTextChangedListener(this);
Enrico Ros1f5a0952014-11-18 20:15:48 -0800113 mMore = findViewById(R.id.more_browser_settings);
114 mMore.setOnClickListener(this);
115 mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
Pankaj Garg32e1b942015-06-03 18:13:24 -0700116 mFaviconBadge = (ImageView) findViewById(R.id.favicon_badge);
John Reck0f602f32011-07-07 15:38:43 -0700117 }
118
119 public void setTitleBar(TitleBar titleBar) {
120 mTitleBar = titleBar;
121 mBaseUi = mTitleBar.getUi();
122 mUiController = mTitleBar.getUiController();
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700123 mUrlInput.setController(mUiController);
John Reck0f602f32011-07-07 15:38:43 -0700124 }
125
Pankaj Garg32e1b942015-06-03 18:13:24 -0700126 public void setLock(Drawable d, Tab.SecurityState securityState) {
127 mSecurityState = securityState;
128 switch (mSecurityState) {
129 case SECURITY_STATE_SECURE:
130 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_good);
131 break;
132 case SECURITY_STATE_MIXED:
133 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_warning);
134 break;
135 case SECURITY_STATE_BAD_CERTIFICATE:
136 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_severe);
137 break;
138 case SECURITY_STATE_NOT_SECURE:
139 default:
140 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_normal);
141 }
John Reck0f602f32011-07-07 15:38:43 -0700142 if (mLockIcon == null) return;
143 if (d == null) {
144 mLockIcon.setVisibility(View.GONE);
145 } else {
146 mLockIcon.setImageDrawable(d);
147 mLockIcon.setVisibility(View.VISIBLE);
148 }
149 }
150
Pankaj Garg32e1b942015-06-03 18:13:24 -0700151 public static int adjustColor(int color, float hueMultiplier,
152 float saturationMultiplier, float valueMultiplier) {
153 float[] hsv = new float[3];
154 Color.colorToHSV(color, hsv);
155 hsv[0] *= hueMultiplier;
156 hsv[1] *= saturationMultiplier;
157 hsv[2] *= valueMultiplier;
158 return Color.HSVToColor(Color.alpha(color), hsv);
159 }
160
161 public static void setStatusAndNavigationBarColor(final Activity activity, int color) {
162 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
163 int currentColor = activity.getWindow().getStatusBarColor();
164 Integer from = currentColor;
165 Integer to = color;
166 ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
167
168 if (mDefaultStatusBarColor == -1) {
169 mDefaultStatusBarColor = activity.getWindow().getStatusBarColor();
170 }
171
172 animator.addUpdateListener(
173 new ValueAnimator.AnimatorUpdateListener() {
174 @Override
175 public void onAnimationUpdate(ValueAnimator animation) {
176 Integer value = (Integer) animation.getAnimatedValue();
177 activity.getWindow().setStatusBarColor(value.intValue());
178 //activity.getWindow().setNavigationBarColor(value.intValue());
179 }
180 }
181 );
182 animator.start();
183 }
184 }
185
186 private void updateSiteIconColor(String urlString, int color) {
187 try {
188 URL url = new URL(urlString);
189 String host = url.getHost();
190 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
191 int currentColor = prefs.getInt(host + ":color", 0);
192 if (currentColor != color) {
193 SharedPreferences.Editor editor = prefs.edit();
194 editor.remove(host + ":color");
195 editor.putInt(host + ":color", color);
196 editor.commit();
197 }
198 } catch (MalformedURLException e) {
199 }
200 }
201
202 public static int getSiteIconColor(String urlString) {
203 try {
204 URL url = new URL(urlString);
205 String host = url.getHost();
206 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
207 return prefs.getInt(host + ":color", 0);
208 } catch (MalformedURLException e) {
209 return 0;
210 }
211 }
212
213 public static int getDefaultStatusBarColor() {
214 return mDefaultStatusBarColor;
215 }
216
John Reck0f602f32011-07-07 15:38:43 -0700217 public void setFavicon(Bitmap icon) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700218 int color = ColorUtils.getDominantColorForBitmap(icon);
219 Tab tab = mUiController.getCurrentTab();
220
221 if (tab != null) {
222 if (tab.hasFavicon()) {
223 updateSiteIconColor(tab.getUrl(), color);
224 setStatusAndNavigationBarColor(mUiController.getActivity(),
225 adjustColor(color, 1, 1, 0.7f));
226 } else {
227 color = getSiteIconColor(tab.getUrl());
228 if (color != 0) {
229 setStatusAndNavigationBarColor(mUiController.getActivity(),
230 adjustColor(color, 1, 1, 0.7f));
231 } else {
232 setStatusAndNavigationBarColor(mUiController.getActivity(),
233 mDefaultStatusBarColor);
234 }
235 }
236 } else {
237 setStatusAndNavigationBarColor(mUiController.getActivity(), mDefaultStatusBarColor);
238 }
239
John Reck0f602f32011-07-07 15:38:43 -0700240 if (mFavicon == null) return;
241 mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon));
242 }
243
Pankaj Garg32e1b942015-06-03 18:13:24 -0700244 protected void showSiteSpecificSettings() {
245 WebView wv = mUiController.getCurrentTopWebView();
246 int count = 0;
247
248 if (wv != null && WebRefiner.getInstance() != null) {
249 count = WebRefiner.getInstance().getBlockedURLCount(wv);
250 }
251
252 Bundle bundle = new Bundle();
253 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE,
254 mUiController.getCurrentTab().getUrl());
255 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_INFO, count);
256
257 bundle.putParcelable(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT,
258 SslCertificate.saveState(wv.getCertificate()));
259
260 SslError error = mUiController.getCurrentTab().getSslCertificateError();
261 if (error != null) {
262 int certError = 0;
263 if (error.hasError(SslError.SSL_DATE_INVALID)) {
264 certError |= (1 << SslError.SSL_DATE_INVALID);
265 }
266
267 if (error.hasError(SslError.SSL_EXPIRED)) {
268 certError |= (1 << SslError.SSL_EXPIRED);
269 }
270
271 if (error.hasError(SslError.SSL_IDMISMATCH)) {
272 certError |= (1 << SslError.SSL_IDMISMATCH);
273 }
274
275 if (error.hasError(SslError.SSL_INVALID)) {
276 certError |= (1 << SslError.SSL_INVALID);
277 }
278
279 if (error.hasError(SslError.SSL_NOTYETVALID)) {
280 certError |= (1 << SslError.SSL_NOTYETVALID);
281 }
282
283 if (error.hasError(SslError.SSL_UNTRUSTED)) {
284 certError |= (1 << SslError.SSL_UNTRUSTED);
285 }
286
287 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_ERR, certError);
288 }
289
290 Bitmap favicon = mUiController.getCurrentTab().getFavicon();
291 if (favicon != null) {
292 ByteArrayOutputStream baos = new ByteArrayOutputStream();
293 favicon.compress(Bitmap.CompressFormat.PNG, 50, baos);
294 bundle.putByteArray(SiteSpecificPreferencesFragment.EXTRA_FAVICON,
295 baos.toByteArray());
296 }
297 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(
298 mUiController.getActivity(),
299 SiteSpecificPreferencesFragment.class.getName(), bundle, 0);
300 }
301
John Reck0f602f32011-07-07 15:38:43 -0700302 @Override
303 public void onClick(View v) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800304 if (mMore == v) {
305 showMenu(mMore);
306 }
307 }
308
309 void showMenu(View anchor) {
310 Activity activity = mUiController.getActivity();
311 if (mPopupMenu == null) {
312 mPopupMenu = new PopupMenu(getContext(), anchor);
313 mPopupMenu.setOnMenuItemClickListener(this);
314 mPopupMenu.setOnDismissListener(this);
315 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
316 mPopupMenu = null;
317 return;
318 }
319 }
320 Menu menu = mPopupMenu.getMenu();
321
322 if (mUiController instanceof Controller) {
323 Controller controller = (Controller) mUiController;
324 if (controller.onPrepareOptionsMenu(menu)) {
325 mOverflowMenuShowing = true;
326 }
327 }
John Reck0f602f32011-07-07 15:38:43 -0700328 }
329
330 @Override
331 public void onFocusChange(View view, boolean hasFocus) {
332 // if losing focus and not in touch mode, leave as is
333 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
334 setFocusState(hasFocus);
335 }
336 if (hasFocus) {
Kevin Hart055e6d82014-12-19 15:53:19 -0800337 Engine.warmUpChildProcessAsync(mUiController.getActivity().getApplicationContext());
John Reck0f602f32011-07-07 15:38:43 -0700338 mBaseUi.showTitleBar();
John Reck0f602f32011-07-07 15:38:43 -0700339 } else if (!mUrlInput.needsUpdate()) {
340 mUrlInput.dismissDropDown();
341 mUrlInput.hideIME();
342 if (mUrlInput.getText().length() == 0) {
343 Tab currentTab = mUiController.getTabControl().getCurrentTab();
344 if (currentTab != null) {
John Reck434e9f82011-08-10 18:16:52 -0700345 setDisplayTitle(currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700346 }
347 }
348 mBaseUi.suggestHideTitleBar();
349 }
350 mUrlInput.clearNeedsUpdate();
351 }
352
353 protected void setFocusState(boolean focus) {
354 }
355
John Reck0f602f32011-07-07 15:38:43 -0700356 public boolean isEditingUrl() {
357 return mUrlInput.hasFocus();
358 }
359
360 void stopEditingUrl() {
Michael Kolb0b129122012-06-04 16:31:58 -0700361 WebView currentTopWebView = mUiController.getCurrentTopWebView();
362 if (currentTopWebView != null) {
363 currentTopWebView.requestFocus();
364 }
John Reck0f602f32011-07-07 15:38:43 -0700365 }
366
367 void setDisplayTitle(String title) {
368 if (!isEditingUrl()) {
Michael Kolb29aab402012-05-29 17:22:35 -0700369 if (!title.equals(mUrlInput.getText().toString())) {
370 mUrlInput.setText(title, false);
371 }
John Reck0f602f32011-07-07 15:38:43 -0700372 }
373 }
374
John Reck0f602f32011-07-07 15:38:43 -0700375 void setIncognitoMode(boolean incognito) {
376 mUrlInput.setIncognitoMode(incognito);
377 }
378
379 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000380 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700381 }
382
383 // UrlInputListener implementation
384
385 /**
386 * callback from suggestion dropdown
387 * user selected a suggestion
388 */
389 @Override
390 public void onAction(String text, String extra, String source) {
Michael Kolb0b129122012-06-04 16:31:58 -0700391 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700392 if (UrlInputView.TYPED.equals(source)) {
kaiyizc4ada322013-07-30 09:58:07 +0800393 String url = null;
Panos Thomas4bdb5252014-11-13 16:20:11 -0800394 boolean wap2estore = BrowserConfig.getInstance(getContext())
395 .hasFeature(BrowserConfig.Feature.WAP2ESTORE);
Vivek Sekhar5e631472014-03-31 23:59:10 -0700396 if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text)
397 || isMakeCallTypeUrl(text)) {
kaiyizc4ada322013-07-30 09:58:07 +0800398 url = text;
399 } else {
400 url = UrlUtils.smartUrlFilter(text, false);
401 }
402
John Reck0f602f32011-07-07 15:38:43 -0700403 Tab t = mBaseUi.getActiveTab();
404 // Only shortcut javascript URIs for now, as there is special
405 // logic in UrlHandler for other schemas
406 if (url != null && t != null && url.startsWith("javascript:")) {
407 mUiController.loadUrl(t, url);
408 setDisplayTitle(text);
409 return;
410 }
kaiyizc4ada322013-07-30 09:58:07 +0800411
412 // add for carrier wap2estore feature
413 if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700414 if (handleEstoreTypeUrl(url)) {
415 setDisplayTitle(text);
416 return;
417 }
kaiyizc4ada322013-07-30 09:58:07 +0800418 }
kaiyiz950eca22013-08-08 11:01:28 +0800419 // add for rtsp scheme feature
420 if (url != null && t != null && isRtspTypeUrl(url)) {
421 if (handleRtspTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700422 setDisplayTitle(text);
kaiyiz950eca22013-08-08 11:01:28 +0800423 return;
424 }
425 }
Vivek Sekhar5e631472014-03-31 23:59:10 -0700426 // add for "wtai://wp/mc;" scheme feature
427 if (url != null && t != null && isMakeCallTypeUrl(url)) {
428 if (handleMakeCallTypeUrl(url)) {
429 return;
430 }
431 }
John Reck0f602f32011-07-07 15:38:43 -0700432 }
433 Intent i = new Intent();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700434 String action = Intent.ACTION_SEARCH;
John Reck0f602f32011-07-07 15:38:43 -0700435 i.setAction(action);
436 i.putExtra(SearchManager.QUERY, text);
437 if (extra != null) {
438 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
439 }
440 if (source != null) {
441 Bundle appData = new Bundle();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800442 appData.putString("source", source);
443 i.putExtra("source", appData);
John Reck0f602f32011-07-07 15:38:43 -0700444 }
445 mUiController.handleNewIntent(i);
446 setDisplayTitle(text);
447 }
448
Vivek Sekhar5e631472014-03-31 23:59:10 -0700449 private boolean isMakeCallTypeUrl(String url) {
450 String utf8Url = null;
451 try {
452 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
453 } catch (UnsupportedEncodingException e) {
454 Log.e(TAG, "err " + e);
455 }
456 if (utf8Url != null && utf8Url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
457 return true;
458 }
459 return false;
460 }
461
462 private boolean handleMakeCallTypeUrl(String url) {
463 // wtai://wp/mc;number
464 // number=string(phone-number)
465 if (url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
466 Intent intent = new Intent(Intent.ACTION_VIEW,
467 Uri.parse(WebView.SCHEME_TEL +
468 url.substring(UrlHandler.SCHEME_WTAI_MC.length())));
469 getContext().startActivity(intent);
470 // before leaving BrowserActivity, close the empty child tab.
471 // If a new tab is created through JavaScript open to load this
472 // url, we would like to close it as we will load this url in a
473 // different Activity.
474 Tab current = mUiController.getCurrentTab();
475 if (current != null
476 && current.getWebView().copyBackForwardList().getSize() == 0) {
477 mUiController.closeCurrentTab();
478 }
479 return true;
480 }
481 return false;
482 }
483
kaiyizc4ada322013-07-30 09:58:07 +0800484 private boolean isEstoreTypeUrl(String url) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700485 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800486 return true;
487 }
488 return false;
489 }
490
Vivek Sekharb54614f2014-05-01 19:03:37 -0700491 private boolean handleEstoreTypeUrl(String url) {
492 if (url.getBytes().length > 256) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800493 Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700494 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800495 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700496
497 Intent intent;
498 // perform generic parsing of the URI to turn it into an Intent.
499 try {
500 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
501 } catch (URISyntaxException ex) {
502 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
503 return false;
504 }
505
kaiyizc4ada322013-07-30 09:58:07 +0800506 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800507 getContext().startActivity(intent);
kaiyizc4ada322013-07-30 09:58:07 +0800508 } catch (ActivityNotFoundException ex) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800509 String downloadUrl = getContext().getResources().getString(R.string.estore_homepage);
kaiyizc4ada322013-07-30 09:58:07 +0800510 mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800511 Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800512 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700513
514 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800515 }
516
kaiyiz950eca22013-08-08 11:01:28 +0800517 private boolean isRtspTypeUrl(String url) {
518 String utf8Url = null;
519 try {
520 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
521 } catch (UnsupportedEncodingException e) {
522 Log.e(TAG, "err " + e);
523 }
524 if (utf8Url != null && utf8Url.startsWith("rtsp://")) {
525 return true;
526 }
527 return false;
528 }
529
530 private boolean handleRtspTypeUrl(String url) {
531 Intent intent;
532 // perform generic parsing of the URI to turn it into an Intent.
533 try {
534 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
535 } catch (URISyntaxException ex) {
536 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
537 return false;
538 }
539
540 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800541 getContext().startActivity(intent);
kaiyiz950eca22013-08-08 11:01:28 +0800542 } catch (ActivityNotFoundException ex) {
543 Log.w("Browser", "No resolveActivity " + url);
544 return false;
545 }
546 return true;
547 }
548
John Reck0f602f32011-07-07 15:38:43 -0700549 @Override
550 public void onDismiss() {
551 final Tab currentTab = mBaseUi.getActiveTab();
552 mBaseUi.hideTitleBar();
553 post(new Runnable() {
554 public void run() {
555 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700556 if (currentTab != null) {
John Reck0f602f32011-07-07 15:38:43 -0700557 setDisplayTitle(currentTab.getUrl());
558 }
559 }
560 });
561 }
562
563 /**
564 * callback from the suggestion dropdown
565 * copy text to input field and stay in edit mode
566 */
567 @Override
568 public void onCopySuggestion(String text) {
569 mUrlInput.setText(text, true);
570 if (text != null) {
571 mUrlInput.setSelection(text.length());
572 }
573 }
574
575 public void setCurrentUrlIsBookmark(boolean isBookmark) {
576 }
577
578 @Override
579 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
580 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
581 // catch back key in order to do slightly more cleanup than usual
Michael Kolb0b129122012-06-04 16:31:58 -0700582 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700583 }
584 return super.dispatchKeyEventPreIme(evt);
585 }
586
John Reck0f602f32011-07-07 15:38:43 -0700587 /**
588 * called from the Ui when the user wants to edit
589 * @param clearInput clear the input field
590 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700591 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700592 // editing takes preference of progress
593 setVisibility(View.VISIBLE);
John Reck0f602f32011-07-07 15:38:43 -0700594 if (!mUrlInput.hasFocus()) {
595 mUrlInput.requestFocus();
596 }
597 if (clearInput) {
598 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700599 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700600 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700601 mUrlInput.showIME();
602 }
John Reck0f602f32011-07-07 15:38:43 -0700603 }
604
605 public void onProgressStarted() {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700606 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_normal);
607 mSecurityState = Tab.SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck0f602f32011-07-07 15:38:43 -0700608 }
609
610 public void onProgressStopped() {
611 }
612
John Reck419f6b42011-08-16 16:10:51 -0700613 public void onTabDataChanged(Tab tab) {
614 }
615
Michael Kolb0b129122012-06-04 16:31:58 -0700616 public void onVoiceResult(String s) {
617 startEditingUrl(true, true);
618 onCopySuggestion(s);
619 }
620
Narayan Kamathf3174a52011-11-17 14:43:32 +0000621 @Override
622 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
623
624 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700625 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000626
627 @Override
628 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700629
Enrico Ros1f5a0952014-11-18 20:15:48 -0800630 @Override
631 public void onStateChanged(int state) {
632 switch(state) {
633 case STATE_NORMAL:
634 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
635 break;
636 case STATE_HIGHLIGHTED:
637 mMore.setVisibility(View.GONE);
638 break;
639 case STATE_EDITED:
640 mMore.setVisibility(View.GONE);
641 break;
642 }
643 }
644
645 public boolean isMenuShowing() {
646 return mOverflowMenuShowing;
647 }
648
649
650 @Override
651 public void onDismiss(PopupMenu popupMenu) {
652 if (popupMenu == mPopupMenu) {
653 onMenuHidden();
654 }
655 }
656
657 private void onMenuHidden() {
658 mOverflowMenuShowing = false;
659 mBaseUi.showTitleBarForDuration();
660 }
661
662
663 @Override
664 public boolean onMenuItemClick(MenuItem item) {
665 return mUiController.onOptionsItemSelected(item);
666 }
John Reck0f602f32011-07-07 15:38:43 -0700667}