blob: d247066f7c9aa5bd2adc56f572e25bbc2b91c681 [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;
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700128 if (mFaviconBadge != null) {
129 switch (mSecurityState) {
130 case SECURITY_STATE_SECURE:
131 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_good);
132 break;
133 case SECURITY_STATE_MIXED:
134 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_warning);
135 break;
136 case SECURITY_STATE_BAD_CERTIFICATE:
137 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_severe);
138 break;
139 case SECURITY_STATE_NOT_SECURE:
140 default:
141 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_normal);
142 }
Pankaj Garg32e1b942015-06-03 18:13:24 -0700143 }
John Reck0f602f32011-07-07 15:38:43 -0700144 if (mLockIcon == null) return;
145 if (d == null) {
146 mLockIcon.setVisibility(View.GONE);
147 } else {
148 mLockIcon.setImageDrawable(d);
149 mLockIcon.setVisibility(View.VISIBLE);
150 }
151 }
152
Pankaj Garg32e1b942015-06-03 18:13:24 -0700153 public static int adjustColor(int color, float hueMultiplier,
154 float saturationMultiplier, float valueMultiplier) {
155 float[] hsv = new float[3];
156 Color.colorToHSV(color, hsv);
157 hsv[0] *= hueMultiplier;
158 hsv[1] *= saturationMultiplier;
159 hsv[2] *= valueMultiplier;
160 return Color.HSVToColor(Color.alpha(color), hsv);
161 }
162
163 public static void setStatusAndNavigationBarColor(final Activity activity, int color) {
164 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
165 int currentColor = activity.getWindow().getStatusBarColor();
166 Integer from = currentColor;
167 Integer to = color;
168 ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
169
170 if (mDefaultStatusBarColor == -1) {
171 mDefaultStatusBarColor = activity.getWindow().getStatusBarColor();
172 }
173
174 animator.addUpdateListener(
175 new ValueAnimator.AnimatorUpdateListener() {
176 @Override
177 public void onAnimationUpdate(ValueAnimator animation) {
178 Integer value = (Integer) animation.getAnimatedValue();
179 activity.getWindow().setStatusBarColor(value.intValue());
180 //activity.getWindow().setNavigationBarColor(value.intValue());
181 }
182 }
183 );
184 animator.start();
185 }
186 }
187
188 private void updateSiteIconColor(String urlString, int color) {
189 try {
190 URL url = new URL(urlString);
191 String host = url.getHost();
192 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
193 int currentColor = prefs.getInt(host + ":color", 0);
194 if (currentColor != color) {
195 SharedPreferences.Editor editor = prefs.edit();
196 editor.remove(host + ":color");
197 editor.putInt(host + ":color", color);
198 editor.commit();
199 }
200 } catch (MalformedURLException e) {
201 }
202 }
203
204 public static int getSiteIconColor(String urlString) {
205 try {
206 URL url = new URL(urlString);
207 String host = url.getHost();
208 SharedPreferences prefs = BrowserSettings.getInstance().getPreferences();
209 return prefs.getInt(host + ":color", 0);
210 } catch (MalformedURLException e) {
211 return 0;
212 }
213 }
214
215 public static int getDefaultStatusBarColor() {
216 return mDefaultStatusBarColor;
217 }
218
John Reck0f602f32011-07-07 15:38:43 -0700219 public void setFavicon(Bitmap icon) {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700220 int color = ColorUtils.getDominantColorForBitmap(icon);
221 Tab tab = mUiController.getCurrentTab();
222
223 if (tab != null) {
224 if (tab.hasFavicon()) {
225 updateSiteIconColor(tab.getUrl(), color);
226 setStatusAndNavigationBarColor(mUiController.getActivity(),
227 adjustColor(color, 1, 1, 0.7f));
228 } else {
229 color = getSiteIconColor(tab.getUrl());
230 if (color != 0) {
231 setStatusAndNavigationBarColor(mUiController.getActivity(),
232 adjustColor(color, 1, 1, 0.7f));
233 } else {
234 setStatusAndNavigationBarColor(mUiController.getActivity(),
235 mDefaultStatusBarColor);
236 }
237 }
238 } else {
239 setStatusAndNavigationBarColor(mUiController.getActivity(), mDefaultStatusBarColor);
240 }
241
John Reck0f602f32011-07-07 15:38:43 -0700242 if (mFavicon == null) return;
243 mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon));
244 }
245
Pankaj Garg32e1b942015-06-03 18:13:24 -0700246 protected void showSiteSpecificSettings() {
247 WebView wv = mUiController.getCurrentTopWebView();
248 int count = 0;
249
250 if (wv != null && WebRefiner.getInstance() != null) {
251 count = WebRefiner.getInstance().getBlockedURLCount(wv);
252 }
253
254 Bundle bundle = new Bundle();
255 bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE,
256 mUiController.getCurrentTab().getUrl());
257 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_INFO, count);
258
259 bundle.putParcelable(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT,
260 SslCertificate.saveState(wv.getCertificate()));
261
262 SslError error = mUiController.getCurrentTab().getSslCertificateError();
263 if (error != null) {
264 int certError = 0;
265 if (error.hasError(SslError.SSL_DATE_INVALID)) {
266 certError |= (1 << SslError.SSL_DATE_INVALID);
267 }
268
269 if (error.hasError(SslError.SSL_EXPIRED)) {
270 certError |= (1 << SslError.SSL_EXPIRED);
271 }
272
273 if (error.hasError(SslError.SSL_IDMISMATCH)) {
274 certError |= (1 << SslError.SSL_IDMISMATCH);
275 }
276
277 if (error.hasError(SslError.SSL_INVALID)) {
278 certError |= (1 << SslError.SSL_INVALID);
279 }
280
281 if (error.hasError(SslError.SSL_NOTYETVALID)) {
282 certError |= (1 << SslError.SSL_NOTYETVALID);
283 }
284
285 if (error.hasError(SslError.SSL_UNTRUSTED)) {
286 certError |= (1 << SslError.SSL_UNTRUSTED);
287 }
288
289 bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT_ERR, certError);
290 }
291
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700292 Bitmap favicon = mUiController.getCurrentTopWebView().getFavicon();
Pankaj Garg32e1b942015-06-03 18:13:24 -0700293 if (favicon != null) {
294 ByteArrayOutputStream baos = new ByteArrayOutputStream();
295 favicon.compress(Bitmap.CompressFormat.PNG, 50, baos);
296 bundle.putByteArray(SiteSpecificPreferencesFragment.EXTRA_FAVICON,
297 baos.toByteArray());
298 }
299 BrowserPreferencesPage.startPreferenceFragmentExtraForResult(
300 mUiController.getActivity(),
301 SiteSpecificPreferencesFragment.class.getName(), bundle, 0);
302 }
303
John Reck0f602f32011-07-07 15:38:43 -0700304 @Override
305 public void onClick(View v) {
Enrico Ros1f5a0952014-11-18 20:15:48 -0800306 if (mMore == v) {
307 showMenu(mMore);
308 }
309 }
310
311 void showMenu(View anchor) {
312 Activity activity = mUiController.getActivity();
313 if (mPopupMenu == null) {
314 mPopupMenu = new PopupMenu(getContext(), anchor);
315 mPopupMenu.setOnMenuItemClickListener(this);
316 mPopupMenu.setOnDismissListener(this);
317 if (!activity.onCreateOptionsMenu(mPopupMenu.getMenu())) {
318 mPopupMenu = null;
319 return;
320 }
321 }
322 Menu menu = mPopupMenu.getMenu();
323
324 if (mUiController instanceof Controller) {
325 Controller controller = (Controller) mUiController;
326 if (controller.onPrepareOptionsMenu(menu)) {
327 mOverflowMenuShowing = true;
328 }
329 }
John Reck0f602f32011-07-07 15:38:43 -0700330 }
331
332 @Override
333 public void onFocusChange(View view, boolean hasFocus) {
334 // if losing focus and not in touch mode, leave as is
335 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
336 setFocusState(hasFocus);
337 }
338 if (hasFocus) {
Kevin Hart055e6d82014-12-19 15:53:19 -0800339 Engine.warmUpChildProcessAsync(mUiController.getActivity().getApplicationContext());
John Reck0f602f32011-07-07 15:38:43 -0700340 mBaseUi.showTitleBar();
John Reck0f602f32011-07-07 15:38:43 -0700341 } else if (!mUrlInput.needsUpdate()) {
342 mUrlInput.dismissDropDown();
343 mUrlInput.hideIME();
344 if (mUrlInput.getText().length() == 0) {
345 Tab currentTab = mUiController.getTabControl().getCurrentTab();
346 if (currentTab != null) {
John Reck434e9f82011-08-10 18:16:52 -0700347 setDisplayTitle(currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700348 }
349 }
350 mBaseUi.suggestHideTitleBar();
351 }
352 mUrlInput.clearNeedsUpdate();
353 }
354
355 protected void setFocusState(boolean focus) {
356 }
357
John Reck0f602f32011-07-07 15:38:43 -0700358 public boolean isEditingUrl() {
359 return mUrlInput.hasFocus();
360 }
361
362 void stopEditingUrl() {
Michael Kolb0b129122012-06-04 16:31:58 -0700363 WebView currentTopWebView = mUiController.getCurrentTopWebView();
364 if (currentTopWebView != null) {
365 currentTopWebView.requestFocus();
366 }
John Reck0f602f32011-07-07 15:38:43 -0700367 }
368
369 void setDisplayTitle(String title) {
370 if (!isEditingUrl()) {
Michael Kolb29aab402012-05-29 17:22:35 -0700371 if (!title.equals(mUrlInput.getText().toString())) {
372 mUrlInput.setText(title, false);
373 }
John Reck0f602f32011-07-07 15:38:43 -0700374 }
375 }
376
John Reck0f602f32011-07-07 15:38:43 -0700377 void setIncognitoMode(boolean incognito) {
378 mUrlInput.setIncognitoMode(incognito);
379 }
380
381 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000382 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700383 }
384
385 // UrlInputListener implementation
386
387 /**
388 * callback from suggestion dropdown
389 * user selected a suggestion
390 */
391 @Override
392 public void onAction(String text, String extra, String source) {
Michael Kolb0b129122012-06-04 16:31:58 -0700393 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700394 if (UrlInputView.TYPED.equals(source)) {
kaiyizc4ada322013-07-30 09:58:07 +0800395 String url = null;
Panos Thomas4bdb5252014-11-13 16:20:11 -0800396 boolean wap2estore = BrowserConfig.getInstance(getContext())
397 .hasFeature(BrowserConfig.Feature.WAP2ESTORE);
Vivek Sekhar5e631472014-03-31 23:59:10 -0700398 if ((wap2estore && isEstoreTypeUrl(text)) || isRtspTypeUrl(text)
399 || isMakeCallTypeUrl(text)) {
kaiyizc4ada322013-07-30 09:58:07 +0800400 url = text;
401 } else {
402 url = UrlUtils.smartUrlFilter(text, false);
403 }
404
John Reck0f602f32011-07-07 15:38:43 -0700405 Tab t = mBaseUi.getActiveTab();
406 // Only shortcut javascript URIs for now, as there is special
407 // logic in UrlHandler for other schemas
408 if (url != null && t != null && url.startsWith("javascript:")) {
409 mUiController.loadUrl(t, url);
410 setDisplayTitle(text);
411 return;
412 }
kaiyizc4ada322013-07-30 09:58:07 +0800413
414 // add for carrier wap2estore feature
415 if (url != null && t != null && wap2estore && isEstoreTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700416 if (handleEstoreTypeUrl(url)) {
417 setDisplayTitle(text);
418 return;
419 }
kaiyizc4ada322013-07-30 09:58:07 +0800420 }
kaiyiz950eca22013-08-08 11:01:28 +0800421 // add for rtsp scheme feature
422 if (url != null && t != null && isRtspTypeUrl(url)) {
423 if (handleRtspTypeUrl(url)) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700424 setDisplayTitle(text);
kaiyiz950eca22013-08-08 11:01:28 +0800425 return;
426 }
427 }
Vivek Sekhar5e631472014-03-31 23:59:10 -0700428 // add for "wtai://wp/mc;" scheme feature
429 if (url != null && t != null && isMakeCallTypeUrl(url)) {
430 if (handleMakeCallTypeUrl(url)) {
431 return;
432 }
433 }
John Reck0f602f32011-07-07 15:38:43 -0700434 }
435 Intent i = new Intent();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700436 String action = Intent.ACTION_SEARCH;
John Reck0f602f32011-07-07 15:38:43 -0700437 i.setAction(action);
438 i.putExtra(SearchManager.QUERY, text);
439 if (extra != null) {
440 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
441 }
442 if (source != null) {
443 Bundle appData = new Bundle();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800444 appData.putString("source", source);
445 i.putExtra("source", appData);
John Reck0f602f32011-07-07 15:38:43 -0700446 }
447 mUiController.handleNewIntent(i);
448 setDisplayTitle(text);
449 }
450
Vivek Sekhar5e631472014-03-31 23:59:10 -0700451 private boolean isMakeCallTypeUrl(String url) {
452 String utf8Url = null;
453 try {
454 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
455 } catch (UnsupportedEncodingException e) {
456 Log.e(TAG, "err " + e);
457 }
458 if (utf8Url != null && utf8Url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
459 return true;
460 }
461 return false;
462 }
463
464 private boolean handleMakeCallTypeUrl(String url) {
465 // wtai://wp/mc;number
466 // number=string(phone-number)
467 if (url.startsWith(UrlHandler.SCHEME_WTAI_MC)) {
468 Intent intent = new Intent(Intent.ACTION_VIEW,
469 Uri.parse(WebView.SCHEME_TEL +
470 url.substring(UrlHandler.SCHEME_WTAI_MC.length())));
471 getContext().startActivity(intent);
472 // before leaving BrowserActivity, close the empty child tab.
473 // If a new tab is created through JavaScript open to load this
474 // url, we would like to close it as we will load this url in a
475 // different Activity.
476 Tab current = mUiController.getCurrentTab();
477 if (current != null
478 && current.getWebView().copyBackForwardList().getSize() == 0) {
479 mUiController.closeCurrentTab();
480 }
481 return true;
482 }
483 return false;
484 }
485
kaiyizc4ada322013-07-30 09:58:07 +0800486 private boolean isEstoreTypeUrl(String url) {
Vivek Sekharb54614f2014-05-01 19:03:37 -0700487 if (url != null && url.startsWith("estore:")) {
kaiyizc4ada322013-07-30 09:58:07 +0800488 return true;
489 }
490 return false;
491 }
492
Vivek Sekharb54614f2014-05-01 19:03:37 -0700493 private boolean handleEstoreTypeUrl(String url) {
494 if (url.getBytes().length > 256) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800495 Toast.makeText(getContext(), R.string.estore_url_warning, Toast.LENGTH_LONG).show();
Vivek Sekharb54614f2014-05-01 19:03:37 -0700496 return false;
kaiyizc4ada322013-07-30 09:58:07 +0800497 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700498
499 Intent intent;
500 // perform generic parsing of the URI to turn it into an Intent.
501 try {
502 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
503 } catch (URISyntaxException ex) {
504 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
505 return false;
506 }
507
kaiyizc4ada322013-07-30 09:58:07 +0800508 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800509 getContext().startActivity(intent);
kaiyizc4ada322013-07-30 09:58:07 +0800510 } catch (ActivityNotFoundException ex) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800511 String downloadUrl = getContext().getResources().getString(R.string.estore_homepage);
kaiyizc4ada322013-07-30 09:58:07 +0800512 mUiController.loadUrl(mBaseUi.getActiveTab(), downloadUrl);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800513 Toast.makeText(getContext(), R.string.download_estore_app, Toast.LENGTH_LONG).show();
kaiyizc4ada322013-07-30 09:58:07 +0800514 }
Vivek Sekharb54614f2014-05-01 19:03:37 -0700515
516 return true;
kaiyizc4ada322013-07-30 09:58:07 +0800517 }
518
kaiyiz950eca22013-08-08 11:01:28 +0800519 private boolean isRtspTypeUrl(String url) {
520 String utf8Url = null;
521 try {
522 utf8Url = new String(url.getBytes("UTF-8"), "UTF-8");
523 } catch (UnsupportedEncodingException e) {
524 Log.e(TAG, "err " + e);
525 }
526 if (utf8Url != null && utf8Url.startsWith("rtsp://")) {
527 return true;
528 }
529 return false;
530 }
531
532 private boolean handleRtspTypeUrl(String url) {
533 Intent intent;
534 // perform generic parsing of the URI to turn it into an Intent.
535 try {
536 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
537 } catch (URISyntaxException ex) {
538 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
539 return false;
540 }
541
542 try {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800543 getContext().startActivity(intent);
kaiyiz950eca22013-08-08 11:01:28 +0800544 } catch (ActivityNotFoundException ex) {
545 Log.w("Browser", "No resolveActivity " + url);
546 return false;
547 }
548 return true;
549 }
550
John Reck0f602f32011-07-07 15:38:43 -0700551 @Override
552 public void onDismiss() {
553 final Tab currentTab = mBaseUi.getActiveTab();
554 mBaseUi.hideTitleBar();
555 post(new Runnable() {
556 public void run() {
557 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700558 if (currentTab != null) {
John Reck0f602f32011-07-07 15:38:43 -0700559 setDisplayTitle(currentTab.getUrl());
560 }
561 }
562 });
563 }
564
565 /**
566 * callback from the suggestion dropdown
567 * copy text to input field and stay in edit mode
568 */
569 @Override
570 public void onCopySuggestion(String text) {
571 mUrlInput.setText(text, true);
572 if (text != null) {
573 mUrlInput.setSelection(text.length());
574 }
575 }
576
577 public void setCurrentUrlIsBookmark(boolean isBookmark) {
578 }
579
580 @Override
581 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
582 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
583 // catch back key in order to do slightly more cleanup than usual
Michael Kolb0b129122012-06-04 16:31:58 -0700584 stopEditingUrl();
John Reck0f602f32011-07-07 15:38:43 -0700585 }
586 return super.dispatchKeyEventPreIme(evt);
587 }
588
John Reck0f602f32011-07-07 15:38:43 -0700589 /**
590 * called from the Ui when the user wants to edit
591 * @param clearInput clear the input field
592 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700593 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700594 // editing takes preference of progress
595 setVisibility(View.VISIBLE);
John Reck0f602f32011-07-07 15:38:43 -0700596 if (!mUrlInput.hasFocus()) {
597 mUrlInput.requestFocus();
598 }
599 if (clearInput) {
600 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700601 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700602 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700603 mUrlInput.showIME();
604 }
John Reck0f602f32011-07-07 15:38:43 -0700605 }
606
607 public void onProgressStarted() {
Pankaj Garg32e1b942015-06-03 18:13:24 -0700608 mFaviconBadge.setImageResource(R.drawable.ic_fav_overlay_normal);
609 mSecurityState = Tab.SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck0f602f32011-07-07 15:38:43 -0700610 }
611
612 public void onProgressStopped() {
613 }
614
John Reck419f6b42011-08-16 16:10:51 -0700615 public void onTabDataChanged(Tab tab) {
616 }
617
Michael Kolb0b129122012-06-04 16:31:58 -0700618 public void onVoiceResult(String s) {
619 startEditingUrl(true, true);
620 onCopySuggestion(s);
621 }
622
Narayan Kamathf3174a52011-11-17 14:43:32 +0000623 @Override
624 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
625
626 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700627 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000628
629 @Override
630 public void afterTextChanged(Editable s) { }
Michael Kolb0b129122012-06-04 16:31:58 -0700631
Enrico Ros1f5a0952014-11-18 20:15:48 -0800632 @Override
633 public void onStateChanged(int state) {
634 switch(state) {
635 case STATE_NORMAL:
636 mMore.setVisibility(mNeedsMenu ? View.VISIBLE : View.GONE);
637 break;
638 case STATE_HIGHLIGHTED:
639 mMore.setVisibility(View.GONE);
640 break;
641 case STATE_EDITED:
642 mMore.setVisibility(View.GONE);
643 break;
644 }
645 }
646
647 public boolean isMenuShowing() {
648 return mOverflowMenuShowing;
649 }
650
651
652 @Override
653 public void onDismiss(PopupMenu popupMenu) {
654 if (popupMenu == mPopupMenu) {
655 onMenuHidden();
656 }
657 }
658
659 private void onMenuHidden() {
660 mOverflowMenuShowing = false;
661 mBaseUi.showTitleBarForDuration();
662 }
663
664
665 @Override
666 public boolean onMenuItemClick(MenuItem item) {
667 return mUiController.onOptionsItemSelected(item);
668 }
John Reck0f602f32011-07-07 15:38:43 -0700669}